3

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.

Newbie
  • 1,584
  • 9
  • 33
  • 72
  • cannot decode - this error is because if you try to decode a file you should load the file.getAbsolutePath() , which should give android something like file:/storage/emulated/0/DCIM/POEMS/JPEG_20161214_170251_1637243168.jpg , so here the file:/ is missing so it wont decode image – HourGlass Dec 19 '16 at 05:29
  • Have you noticed this - `Permission denied` – Monish Kamble Dec 19 '16 at 05:30
  • you should specify this permission as well. – HourGlass Dec 19 '16 at 05:32
  • @HourGlass it gives me No such file or directory if I add "file:" into the path – Newbie Dec 19 '16 at 05:39
  • so now you are not getting permission denied error? did you add read_external_Storage permission as well ? if you are not getting permission denied error now. try using the pervious path you have – HourGlass Dec 19 '16 at 05:40
  • @MonishKamble I noticed that, but how to solve? I did include permission – Newbie Dec 19 '16 at 05:40
  • 1
    If you are targeting API 23, you have to request permissions at Runtime. Check this out - https://developer.android.com/training/permissions/requesting.html – Monish Kamble Dec 19 '16 at 05:41
  • @HourGlass exactly, no more `Permission denied`, but now `No such file or directory` – Newbie Dec 19 '16 at 05:41
  • 1
    @MonishKamble thank you, the problem was I didn't request permission at Runtime. – Newbie Dec 19 '16 at 05:46
  • For debugging, grant the `READ_EXTERNAL_STORAGE` or `WRITE_EXTERNAL_STORAGE` permission manually from Settings. If the error resolves, you have to implement Runtime Permissions. Else, post the error log. – Monish Kamble Dec 19 '16 at 05:46
  • I think the way you are directly trying to convert a bitmap a bitmap as drawable is leading you to this error. First decode the file as bitmap Bitmap bitmap = BitmapFactory.decodeFile(file.getUri.toString(),null); you can also scaled down bitmap using BitmapFactory.options and pass it to while decode.File(file_path,(BitmapFactory )option). This will give you a scaled bitmap – HourGlass Dec 19 '16 at 06:06

1 Answers1

-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");
bharat7777
  • 323
  • 3
  • 15
  • The problem is due to Runtime Permissions as seen in logcat messages. – Monish Kamble Dec 19 '16 at 06:32
  • please set target version to 21 then run your application. – bharat7777 Dec 19 '16 at 06:41
  • Why to lower the target version? That is not a solution. The solution is to implement Runtime Permissions. – Monish Kamble Dec 19 '16 at 06:44
  • Then you need to implement version condition for taking permission to above these code then on the permission access block you can do this. Else i already said to you what you need to do. – bharat7777 Dec 19 '16 at 06:46
  • As commented by the OP, the problem was due to Runtime Permissions. Nothing wrong with the image decoding code. This is not a valid answer. – Monish Kamble Dec 19 '16 at 06:51
  • I suggest you for permission you should implement this.. [link](http://stackoverflow.com/questions/33162152/storage-permission-error-in-marshmallow) – bharat7777 Dec 19 '16 at 06:55