在Samsung Galaxy S4(型号:GT-I9500)上运行一些基本代码时,我遇到了一个具体问题。
我是通过相机或图库实现了一个图像选择器,并且在我的生活中无法弄清楚为什么ImageView在调用时是空白的 -
imageView.setImageURI(uri);
直到我在模拟器中运行完全相同的代码(然后是Nexus 5),才发现这是三星S4问题。
上找到完整的示例项目我使用的代码取自SO post:
在 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;
}
}
}
使用 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;
}
}
使用Bitmap Factory时也会出现
try {
Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在运行4.2.2的三星S4上运行的结果
在运行Android 4.4.4的GenyMotion 2.4.0上运行时的结果
任何人都知道为什么会这样吗?
答案 0 :(得分:3)
所以问题原来是三星S4处理的图像位图太大了。
令人沮丧的是没有引发任何错误 - 正确的解决方案如下:
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;
}
助手方法:
// 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;
}
答案 1 :(得分:1)
使用Picasso时,请使用fit()或resize()。它处理调整位图的大小。
答案 2 :(得分:0)
他们是否有机会使用不同的应用程序? 4.4为图像选择引入了几个新的URI,因为它们可以是本地的,谷歌加等等。
试试这个:BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri))
它返回一个可以在图像视图中用作
的位图imgView.setImageBitmap(bitmap)