相机拍摄的图像不想在 ImageView 中显示
Posted
技术标签:
【中文标题】相机拍摄的图像不想在 ImageView 中显示【英文标题】:Image taken by camera does not want to display in ImageView 【发布时间】: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 发送到相机意图后,它发生了变化。这也修复了从相机加载图片的问题。
【参考方案1】:
我认为错误是在文件创建中
下面的代码对我有用
File photofile = new File(Environment
.getExternalStorageDirectory(),"sample"
+ System.currentTimeMillis() + ".jpg");
photFileUri = Uri.fromFile(photofile);
photoPath = photofile.getAbsolutePath();
Log.v("camera photo path is "," "+photoPath);
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photFileUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent,1);
在OnActivityForResult()方法中你会得到路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
switch (requestCode)
case 1:
//here u can use **photoPath** to display image
【讨论】:
这个并省略mPicture.recycle();
解决了这个问题,谢谢!【参考方案2】:
试试这个:--
Handler handler = new Handler();
handler.posDelayed(new Runnable)
@Override
public void run()
//Your ImageView Code & setImageBitmap
, 20);
【讨论】:
以上是关于相机拍摄的图像不想在 ImageView 中显示的主要内容,如果未能解决你的问题,请参考以下文章