Search code examples
androidbitmapfactory

Android - Using BitmapFactory.decodefile with jpeg files does not work


Context: I'm creating a simple application where the user clicks a button, they choose a picture from their phone and the image will be displayed in the application. I accomplish this by launching an intent for the user to choose an image.

Problem: When the user chooses the picture, if it's a .jpg file, it's not displayed. However, if it's a .png file, it works as expected.

Note: I've verified that the Bitmap result is not null and there are no errors in logcat or any messages from the decodefile method

Any help would be greatly appreciated

The code:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == SELECT_PICTURE)
{
    Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);
    Toast.makeText(getApplicationContext(), "path is " + selectedImagePath, Toast.LENGTH_LONG).show();
    ImageView image = (ImageView)findViewById(R.id.selectedImage);

    if (image == null)
        Toast.makeText(getApplicationContext(), "No image to update", Toast.LENGTH_SHORT).show();

    Bitmap result = BitmapFactory.decodeFile(selectedImagePath);

    if (result == null)
        Toast.makeText(getApplicationContext(), "Couldn't upload image because it's null", Toast.LENGTH_LONG).show();

    image.setImageBitmap(result);
}

}


Solution

  • It's weird, but my code is now working, but I didn't change anything.