I've had a specific issue when running some basic code on a Samsung Galaxy S4 (model: GT-I9500).
I was implementing a image picker via the camera or gallery, and could not for the life of me figure out why the ImageView was blank when calling -
imageView.setImageURI(uri);
It wasn't until I ran the exact same code in the emulator (and then a Nexus 5) that I found that this was a Samsung S4 issue.
Full sample project can be found on Github & ready to run
Code I used was taken from this SO post:
In OnCreate:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[]{"Gallery", "Camera"},
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
//Launching the gallery
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY);
break;
case 1:
//Specify a camera intent
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
//Check to see if there is an SD card mounted
if (android.os.Environment.getExternalStorageState().equals
(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
IMAGEFOLDER);
else
cameraFolder = MainActivity.this.getCacheDir();
if (!cameraFolder.exists())
cameraFolder.mkdirs();
//Appending timestamp to "picture_"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(),
IMAGEFOLDER + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
//Setting a global variable to be used in the OnActivityResult
imageURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, CAMERA);
break;
default:
break;
}
}
});
builder.show();
}
});
OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case GALLERY:
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
break;
case CAMERA:
imageView.setImageURI(imageURI);
break;
}
}
}
Also occurs when using Picasso
if (resultCode == RESULT_OK) {
switch (requestCode) {
case GALLERY:
Uri selectedImage = data.getData();
Picasso.with(context)
.load(selectedImage)
.into(imageView);
break;
case CAMERA:
Picasso.with(context)
.load(imageURI)
.into(imageView);
break;
}
}
Also occurs when using Bitmap Factory
try {
Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
The results when ran on a Samsung S4 running 4.2.2
The results when ran on a GenyMotion 2.4.0 running Android 4.4.4
Anyone know why this happens?