Search code examples
androidandroid-permissionsandroid-file

Android read image files from app


I've created a simple android app that takes in 2 photos and tries its best to compare the 2. The app works fine when taking the pictures inside the app, but I'm having issues trying to load images from my phone. I want to be able to load pictures in that I've taken with my Camera and that are saved on my phone (specifically at /DCIM/Camera from the Android device). The code I have is really basic but it just consists of the following:

File file = new File(Environment.getExternalStorageDirectory(),imagePath);
if(file.exists())
    Bitmap origBitmap = BitmapFactory.decodeFile(imagePath);

However, origBitmap is null, and I am assuming it's because file is not being loaded properly. Since file.exists() is false, I assume my permissions are off. But in my manifest file I have the permission: android.permission.READ_EXTERNAL_STORAGE. I'm just not sure what I'm missing here. Thanks for any help.


Solution

  • Since your target is API 25, you'll need to add in runtime persmission checks to access the user's media.

    See this article for more information: https://developer.android.com/training/permissions/requesting.html

    Alternatively, you can goto Android Settings->Apps and find your app and manually grant the app permission to media. This should get you passed your problem, but you'll want to add the runtime permission checks if you plan on distributing the app.