首先,我的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我保存和获取图像的方法:
private void saveIamgeToLocalStore(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/temp");
myDir.mkdirs();
String fname = "Profile_Image.png";
File file = new File (myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadImageFromLocalStore(String imageURI) {
try {
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().toString() + imageURI);
Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri));
profileImage.setImageBitmap(bitmap);
profileImage.setTag("Other");
select_image_button.setText(R.string.button_remove_profile_picture);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
用法:
saveIamgeToLocalStore(BitmapFactory.decodeFile(picturePath));
loadImageFromLocalStore("/temp/Profile_Image.png");
我得到一个
java.io.FileNotFoundException: No content provider: ...
警告。
我错过了什么?
PS:图片保存在/mnt/sdcard/temp/
. 加载图像时出现警告。