gpt4 book ai didi

java - 相机拍摄的图像不想显示在 ImageView 中

转载 作者:行者123 更新时间:2023-11-30 03:58:09 31 4
gpt4 key购买 nike

我有一个 ImageView,我希望能够从中加载图像:

  1. 图库 - 工作正常

  2. 相机 - 不加载任何东西

使用这两种不同操作时的日志非常相似,除了相机操作显示到最后:

  • 10-21 23:24:18.304: D/android.widget.GridLayout(4447): 水平约束条件:x6 - x0 > 720,x5 - x0 > 720,x5 - x4 < 20,x4 - x3 <142、x3 - x2 < 70、x2 - x1 < 32、x1 - x0 < 138不一致;永久移除:x5 - x4 < 20。
  • 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 < 1140, y3 - y2 < 120, y2 - y1 < 30不一致;永久删除:y4 - y0 < 1140,y3 - y2 < 120。

我有一种感觉,这是为什么图像没有显示的解释。该应用程序没有段错误或任何东西,从用户界面的角度来看没有任何反应。

代码(由相机 Action 和画廊 Action 共享)是:

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());
}
}

路径正确,位图不为空,一切看起来都正常,但图像不显示。感谢您的帮助!

最佳答案

我认为错误是在创建文件时

下面这段代码对我有用

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
}

关于java - 相机拍摄的图像不想显示在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13006326/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com