问题
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?
回答1:
So the problem turns out to be the image bitmaps being too large for the Samsung S4 to handle.
Frustratingly no errors are thrown - The correct solution is as follows:
switch (requestCode) {
case GALLERY:
Bitmap bitmap = createScaledBitmap(getImagePath(data, getApplicationContext()), imageView.getWidth(), imageView.getHeight());
imageView.setImageBitmap(bitmap);
break;
case CAMERA:
String path = imageURI.getPath();
Bitmap bitmapCamera = createScaledBitmap(path, imageView.getWidth(), imageView.getHeight());
imageView.setImageBitmap(bitmapCamera);
break;
}
Helper methods:
// Function to get image path from ImagePicker
public static String getImagePath(Intent data, Context context) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
public Bitmap createScaledBitmap(String pathName, int width, int height) {
final BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, opt);
opt.inSampleSize = calculateBmpSampleSize(opt, width, height);
opt.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, opt);
}
public int calculateBmpSampleSize(BitmapFactory.Options opt, int width, int height) {
final int outHeight = opt.outHeight;
final int outWidth = opt.outWidth;
int sampleSize = 1;
if (outHeight > height || outWidth > width) {
final int heightRatio = Math.round((float) outHeight / (float) height);
final int widthRatio = Math.round((float) outWidth / (float) width);
sampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return sampleSize;
}
回答2:
When using Picasso, use fit() or resize(). It handles resizing the bitmap.
回答3:
Are they using different apps by any chance? 4.4 introduced several new URIs for image selecting, as they can be local, google plus etc.
Try this: BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri))
It returns a Bitmap that can be used in the image view as
imgView.setImageBitmap(bitmap)
来源:https://stackoverflow.com/questions/29932060/imageview-will-not-load-via-setimageuri-when-ran-on-samsung-galaxy-s4-only