Gallery Image File Exists But Bitmap Decode File Is Null
I'm trying to show an image picked from the gallery using the uri path returned by OnActivityResult.The file exists and it is either a .png or .jpg format but Bitmap.decodeFile() i
Solution 1:
What am I doing wrong?
First, Android is case-sensitive, so your <uses-permission>
elements are wrong. Replace ANDROID.PERMISSION
with android.permission
.
Second, there is no requirement for your getRealPathFromURI()
method to work on all devices for all images. Replace:
String imagePath = "";
Uri targetUri = data.getData();
if (data.toString().contains("content:")) {
imagePath = getRealPathFromURI(targetUri);
} else if (data.toString().contains("file:")) {
imagePath = targetUri.getPath();
} else {
imagePath = null;
}
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Log.d("Image",imagePath);
File thePath = new File(imagePath);
if(thePath.exists()){
Log.d("Image", "exists");
}
Bitmap myBitmap = BitmapFactory.decodeFile(thePath.getAbsolutePath());
if(myBitmap == null)
Log.d("Image","bummer");
imageView.setImageBitmap(myBitmap);
with:
Uri targetUri = data.getData();
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap myBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
if(myBitmap == null)
Log.d("Image","bummer");
imageView.setImageBitmap(myBitmap);
Eventually, use an image-loading library (e.g., Picasso, Glide) or otherwise move that work to a background thread.
Solution 2:
Bitmap.decodeFile() is always null
If the bitmap would become too big for available memory then decodeFile() returns null. Like decodeStream() will do then.
Try with a smaller picture. Or scale down while loading.
Post a Comment for "Gallery Image File Exists But Bitmap Decode File Is Null"