问题
I want to save an image into Bitmap by using the absolute path of the image file, below is my code:
Log.d("PhotoPath", selected.getImagePath());
File file = new File(selected.getImagePath());
if(file.exists())
{
Log.d("File", "Exist");
Bitmap d = new BitmapDrawable(getResources(), selected.getImagePath()).getBitmap();
int nh = (int) (d.getHeight() * (512.0 / d.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
iv_Photo.setImageBitmap(scaled);
}
else
Log.d("File", "Not exist");
Below is my output including the exception:
D/PhotoPath: /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg
D/File: Exist
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg (Permission denied)
W/BitmapDrawable: BitmapDrawable cannot decode /storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg
What is the problem? I did add WRITE_EXTERNAL_STORAGE
permission in Manifest.
回答1:
Please see below here, you need to decode file path then you will get bitmap.
Log.d("PhotoPath", selected.getImagePath());
File file = new File(selected.getImagePath());
if(file.exists())
{
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
else
Log.d("File", "Not exist");
来源:https://stackoverflow.com/questions/41216176/unable-to-decode-stream