0

I have encountered a strange problem while working with a bitmap, I have an activity where I have a button where onClick() method is set to selectImage(view) and the method definition is as follows

    public void selectImage(View v){

    Intent imagePickerIntent = new Intent(Intent.ACTION_PICK);
    File imageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    Uri data = Uri.parse(imageDir.getPath());
    imagePickerIntent.setDataAndType(data, "image/*");
    startActivityForResult(imagePickerIntent, 20);

}

and onActivityResult method is as follows:

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode==20){
        if(resultCode==RESULT_OK){
            Uri outputFileDir = data.getData();
            bitmap = BitmapFactory.decodeFile(outputFileDir.getPath(), options);
            String result = getImageNameFromUri(outputFileDir);
            selectedImageTV = (TextView)findViewById(R.id.selectedImageTV);
            selectedImageTV.setText(result);
            image.setImageBitmap(bitmap);
        }
        else{
            return;
        }
    }
}

but decodeFile() method is returning null in Jio Lyf mobile (low resolution) while it is working fine on Xiaomi devices.

How can I assure that decodeFile() method always return decodedBitmap object on all android devices? Please suggest me ways to tackle such problem.

Shahroz Saleem
  • 109
  • 1
  • 10

1 Answers1

0

How can I assure that decodeFile() method always return decodedBitmap object on all android devices?

data.getData() is a Uri. It is not a file. It usually does not point to a file.

Furthermore, I would expect your code to fail on many Android devices, as there is no requirement that there be an ACTION_PICK activity that supports picking from a file Uri, in which case your startActivityForResult() call will crash.

If you want the user to pick a file, integrate a file-picker library.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But I am passing (uri).getPath in the method which will be string as needed by the method. – Shahroz Saleem May 22 '17 at 10:23
  • @ShahrozSaleem: `getPath()` is meaningless for most `Uri` values. For example, `http://stackoverflow.com/questions/44101215/bitmapfactory-decode-methods-is-returning-null-in-some-android-devices/44101242` is a `Uri` value. Its path is `/questions/44101215/bitmapfactory-decode-methods-is-returning-null-in-some-android-devices/44101242`. Do you actually think that every computer on Earth has a file on its local filesystem at that path? A `Uri` is an opaque handle to some content, nothing more. – CommonsWare May 22 '17 at 11:21
  • is there a way to getAbsolutePath of the pickedFile then ? – Shahroz Saleem May 22 '17 at 12:04
  • @ShahrozSaleem: If by `pickedFile`, you are referring to using [a file-picker library](https://android-arsenal.com/tag/35), a well-written library will give you the `File` that was picked, and you can call `getAbsolutePath()` on that `File`. If by `pickedFile` you mean `data.getData()`, then no, as [it is not a file](http://stackoverflow.com/a/38028166/115145), but you are welcome to get an `InputStream` on the content via `openInputStream()` on `ContentResolver`. – CommonsWare May 22 '17 at 12:08