gpt4 book ai didi

android - ImageView 不显示图像

转载 作者:太空狗 更新时间:2023-10-29 14:20:07 24 4
gpt4 key购买 nike

我在我的应用程序中使用相机。我需要拍照并且应该在 imageview 中显示它。我正在使用以下代码从相机拍照并显示它。

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(intent, 0);
imageView.setImageURI(capturedImageUri);

这仅适用于两个或有时三个图像,然后 imageview 不显示图像,但图像正确地存储在 SD 卡中。或者我也用过

   BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
Imageview.setImageBitmap(bitmap);

但是我也遇到了同样的问题。任何人都可以帮助我吗?

最佳答案

capturedImageUri  will return path to captured Image not the actual Image..

此外,重要说明——如果您不需要全尺寸图像,请使用-

   // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);   Comment this line
Bitmap image = (Bitmap) data.getExtras().get("data");

要获取完整尺寸的位图,请使用以下代码-

  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try
{
// place where to store camera taken picture
tempPhoto = createTemporaryFile("picture", ".png");
tempPhoto.delete();
}
catch(Exception e)
{

return ;
}
mImageUri = Uri.fromFile(tempPhoto);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(cameraIntent, 1);

private File createTemporaryFile(String part, String ext) throws Exception
{
// File to store image temperarily.
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
if(!tempDir.exists())
{
tempDir.mkdir();
}
return File.createTempFile(part, ext, tempDir);
}




protected void onActivityResult(int requestCode, int resultCode, Intent data) {

ContentResolver cr = getApplicationContext().getContentResolver();
try {
cr.notifyChange(mImageUri, null);
File imageFile = new File(tempPhoto.getAbsolutePath());


} catch (Exception e) {
e.printStackTrace();
}

Bitmap photo=null;
if (resultCode == 1) {
try {
photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
imageView.setImageBitmap(photo);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (Exception e) {

e.printStackTrace();


}

关于android - ImageView 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894172/

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