Android: decodeFile always returns null for file in internal storage

BitmapFactory.decodeFile expects a file path without the scheme. I.e. without the file:// in the beginning.

If you're handling a Uri, don't just .toString() it, but instead call .getPath() on it, and pass that to the method.


Folks, files stored in app resource should be referenced in special way. E.g. if file is located in assets and named as "myfile.png" it has to be referenced as:

String uriString="file:///android_asset/myfile.png";
Uri uri=Uri.parse(uriString);

This question has been answered before such as here: BitmapFactory.decodeFile returns null even image exists

This was exactly what I needed:

String fname=new File(getFilesDir(), "test.png").getAbsolutePath();

Instead of using BitmapFactory.decodeFile, try using InputStream:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    if(resultCode == RESULT_OK){          
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);