2017-09-05 157 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); 
相关问题