首先,我權限:保存/載入圖像/從本地存儲
<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/
。加載圖像時出現警告。
在 http://stackoverflow.com/questions/18010739/android-save-images-in-an-specific-folder/18010883#18010883 – mdDroid