2017-09-05 126 views
0

我想當用戶註冊到我的應用程序時,他可以拍照或從他的照片庫中選擇他的照片用於應用程序。 我想將此圖片保存在內部應用程序文件夾中,這樣我就不需要詢問存儲權限了(我想!)。 我不能這樣做。 現在,我有圖像的Uri。 我該怎麼做?Android - 在哪裏以及如何保存個人資料圖片

+0

你,如果你打算稍後閱讀圖像可能仍然需要得到許可。我猜想一個想要訪問你的圖片的應用程序需要許可。 –

+0

我敢肯定,如果你谷歌「安卓商店形象在內部應用程序文件夾」,你會發現你需要的所有信息 –

+0

'保存此圖片在內部應用程序文件夾。你認爲什麼是「內部應用文件夾」? – greenapps

回答

1

您可以從URI獲得Bitmap並保存到本地存儲。爲了從Bitmap

public static Bitmap getBitMap(Uri uri) throws FileNotFoundException, IOException{ 


InputStream input = this.getContentResolver().openInputStream(uri); 

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); 
    onlyBoundsOptions.inJustDecodeBounds = true; 
    onlyBoundsOptions.inDither=true;//optional 
    onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional 
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions); 
    input.close(); 

    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) { 
    return null; 
    } 

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth; 

    double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize/THUMBNAIL_SIZE) : 1.0; 

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); 
    bitmapOptions.inDither = true; //optional 
    bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;// 
    input = this.getContentResolver().openInputStream(uri); 
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); 
    input.close(); 
    return bitmap; 
} 

private static int getPowerOfTwoForSampleRatio(double ratio){ 
    int k = Integer.highestOneBit((int)Math.floor(ratio)); 
    if(k==0) return 1; 
    else return k; 
} 

然後調用它像:

Bitmap bitmap = getBitMap(imageUri); 

然後保存位圖到內部存儲創建一個類ImageStorage

public class ImageStorage { 


public static String saveInternalStorage(Context context,Bitmap bitmap, String filename) { 

    String stored = null; 

    File sdcard = context.getFilesDir(); 

    File folder = new File(sdcard.getAbsoluteFile(), "/directoryName/"); 
    folder.mkdir(); 
    File file = new File(folder.getAbsoluteFile(), filename + ".jpg") ; 

    if (file.exists()) 
     return stored ; 

    try { 
     FileOutputStream out = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     out.flush(); 
     out.close(); 
     stored = "success"; 
    } catch (Exception e) { 
     FirebaseCrash.report(e); 
     e.printStackTrace(); 
    } 
    return stored; 
} 

public static File getImage(Context context,String imagename) { 

    File mediaImage = null; 
    try { 
     String root = context.getFilesDir().toString(); 
     File myDir = new File(root); 
     if (!myDir.exists()) 
      return null; 

     mediaImage = new File(myDir.getPath() + "/directoryName/"+imagename); 
    } catch (Exception e) { 
     FirebaseCrash.report(e); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return mediaImage; 
} 
public static boolean checkifImageExists(Context context,String imagename) 
{ 
    Bitmap b = null ; 
    File file = ImageStorage.getImage(context,"/"+imagename+".jpg"); 
    String path = file.getAbsolutePath(); 

    if (path != null) 
     b = BitmapFactory.decodeFile(path); 

    if(b == null || b.equals("")) 
    { 
     return false; 
    } 
    return true ; 
} 
} 

之後調用與功能你的活動Context

+0

'File sdcard = context.getFilesDir();'。 getFilesDir()不是SD卡,而是內存。請重命名您的函數和變量,因爲名稱非常混亂。 – greenapps

+0

'並保存到本地存儲.'應該'並保存到內部存儲。 – greenapps

+0

'folder.mkdir();'如果目錄尚不存在,只調用mkdir()。並檢查返回值,因爲它可能會失敗。 – greenapps

0

如果您將個人資料圖像保存到存儲中,您肯定需要讀取和寫入權限。
我不認爲它是一個好主意,如果你在本地使用它,如你在你的問題中提到的那樣,將圖像存儲在不同的文件夾中。
但是, 如果你想這樣做,不求回報,你可以存儲當前圖像URI(你越來越)存儲許可進入SharedPreferences和使用保存的URI顯示個人資料圖片。
例子:在你的類

申報SharedPrefrence:

SharedPreferences sharedpreferences; 
public static String MYPREFERENCES="MyPrefs"; 

初始化SharedPrefrence裏面的onCreate():

sharedpreferences = getSharedPreferences(MYPREFERENCES, 0); 

使用此sharedprefrence你在哪裏歌廳你圖片的URI和保存在這裏。
例子:

Uri imgUri = Uri.parse("content://x.y.z/"); // Use your URI 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
      editor.putString("ProfileImageUri",imgUri); 
      editor.apply(); 

現在從您要設置或顯示用戶有個人資料相片sharedprefrence檢索該URI。
例子:

Uri defaultImageUri = Uri.parse("android.resource://my.package.name/"+R.drawable.profileimage); // use this default image uri if user didn't saved any image to sharedprefrence . 

    sharedpreferences = getSharedPreferences(MYPREFERENCES, 0); 
    ImageURI= sharedpreferences.getString("ProfileImageUri", defaultImageUri.toString); 


現在,

Uri imgUri = Uri.parse(ImageURI); 
imageView.setImageURI(imgUri); 
相關問題