【发布时间】:2012-10-11 23:04:30
【问题描述】:
我有一个 ImageView,我希望能够从中加载图像:
图库 - 运行良好
相机 - 不加载任何东西
使用这两个不同动作的日志非常相似,除了相机动作显示到最后:
- 10-21 23:24:18.304: D/android.widget.GridLayout(4447): 水平 约束:x6 - x0 > 720, x5 - x0 > 720, x5 - x4
- 10-21 23:24:18.324: D/android.widget.GridLayout(4447): 垂直 约束:y1 - y0 > 468, y2 - y1 > 30, y3 - y2 > 120, y3 - y0 > 1140, y4 - y3 > 612, y4 - y0
我感觉这是为什么图像没有显示的解释。该应用程序没有段错误或任何东西,从 UI 角度来看没有任何反应。
代码(相机动作和画廊动作共享)是:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
if ((requestCode == SELECT_PICTURE) || (requestCode == PICTURE_RESULT)) {
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
mImageView = (ImageView) findViewById(R.id.imageView1);
int x = mImageView.getWidth();
int y = mImageView.getHeight();
if (x == 0 || y == 0) {
Display d = getWindowManager().getDefaultDisplay();
x = d.getWidth();
y = d.getHeight();
}
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, opts);
// Calculate inSampleSize
opts.inSampleSize = calculateInSampleSize(opts, x, y);
// Decode bitmap with inSampleSize set
opts.inJustDecodeBounds = false;
mPicture = BitmapFactory.decodeFile(selectedImagePath, opts);
mPicture.recycle(); //otherwise multiple calls segfault
// create a matrix object
Matrix matrix = new Matrix();
matrix.postRotate(90); // clockwise by 90 degrees
// create a new bitmap from the original using the matrix to transform the result
Bitmap rotatedBitmap = Bitmap.createBitmap(mPicture, 0, 0, mPicture.getWidth(), mPicture.getHeight(), matrix, true);
//set image view
mImageView.setImageBitmap(rotatedBitmap);
} catch (Exception e) {
System.out.println("Bitmap could not be decoded." + e.getMessage());
}
}
路径正确,位图不为空,一切看起来都还可以,但图像不显示。感谢您的帮助!
【问题讨论】:
-
在您仍在使用
mPicture时放置mPicture.recycle();是灾难的根源 - 在线帮助说it should only be called if you are sure there are no further uses for the bitmap,这在您的示例中显然是不正确的。我已经成功了following the instructions here。 -
@KenY-N 谢谢!以前,它给我 OOM 而不使用回收。但是,在按照 NARESH 的建议将 Uri 发送到相机意图后,它发生了变化。这也修复了从相机加载图片的问题。
标签: java android bitmap android-camera android-imageview