gpt4 book ai didi

android - 从相机拍照后图像未在 ImageView 中设置

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

我已经为强制配置开发了自己的相机应用程序,当我尝试在下一个显示是否保存的 Activity 中显示捕获的图像时?

我无法获取我捕获并显示在我的 ImageView 上的图像。我绝对得到了具有正确路径的 absPathUri

代码 fragment :-

imgView = (ImageView)findViewById(R.id.picView);
Bundle b= getIntent().getExtras();
absPathUri = Uri.parse(b.getString("URI"));
Toast.makeText(getApplicationContext(), ""+absPathUri, Toast.LENGTH_SHORT).show();
if(absPathUri!=null)
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));
imgView.setImageURI(absPathUri);
}

在 Further dive in the reason why I'm unable to set the ImageView 中,抛出 Null Pointer Exception 这是由于 File Not Found。如果应用程序处于 Debug模式,它会在 ImageView 上显示正确的图像。似乎 mediaStore 得到刷新,直到调试命中。

File imgFile = new  File(getPath(absPathUri));
Toast.makeText(getApplicationContext(), "ImageFile Exists"+imgFile.exists(), Toast.LENGTH_SHORT).show();
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imgView.setImageBitmap(myBitmap);
}

public String getPath(Uri photoUri) {

String filePath = "";
if (photoUri != null) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try
{

Cursor cursor = getContentResolver().query(photoUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
}catch(Exception e)
{
Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();
}
}
return filePath;
}

尝试过的解决方案:-

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE));

谁能指导我在 ImageView 上显示图像时出错的地方?

最佳答案

正如 gnuanu 所建议的,setImageURI() 不适合在 UI 线程上用作读取和解码,这可能会导致延迟问题。

最好使用以下内容:-

setImageDrawable(android.graphics.drawable.Drawable) 或 setImageBitmap(android.graphics.Bitmap) 和 BitmapFactory 代替。

这些方法仍然没有解决我的问题。当我用相机拍照并点击它发送到下一个 Activity 时,这可能会导致延迟问题。

所以最好在一段时间后转到另一个 Activity 。 .只需一秒钟即可轻松完成。

解决我的问题的 fragment :-

final Intent picIntent = new Intent(CameraActivity.this,PicSaveActivity.class);
picIntent.putExtra("URI", pathUri.toString());
Handler handler = new Handler()
{
public void handleMessage(Message msg) {
startActivityForResult(picIntent, PICTAKEN);
};
};
Message msg = handler.obtainMessage();
handler.sendMessageDelayed(msg, 1000);

在 Next Activity 中,捕获 URI 并像横向模式一样旋转图像。

 if(absPathUri!=null)
{
Bitmap myImg = BitmapFactory.decodeFile(absPathUri.getPath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
matrix, true);
imgView.setImageBitmap(rotated);
}

关于android - 从相机拍照后图像未在 ImageView 中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20921180/

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