Android 图像未使用 setImageURI() 在 ImageView 中设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21158145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:03:34  来源:igfitidea点击:

Image is not setting in ImageView with setImageURI()

androiduriandroid-imageviewandroid-image

提问by Vikalp Patel

Create own camera as I'need Focus ON for taking picture. Camera works perfectly fine as expected. Passing URIbetween activities.

创建自己的相机,因为我需要 Focus ON 来拍照。相机按预期工作得很好。URI在活动之间传递。

I've ImageViewin Next Screen which i used to set the Image with

ImageView在下一个屏幕中,我用来设置图像

imgView.setImageURI(absPathUri);

Checked that absPathUriis not null. Still ImagViewis blank with no pictures. Doing the same in debug mode brings the Picture in ImageView.

检查absPathUri不为空。还是一片ImagView空白,没有图片。在调试模式下做同样的事情会在ImageView.

Can anybody please tell where I'm doing wrong?

谁能告诉我哪里做错了?

Also send broadcast to re-scan the device but still no success with following snippet.

还发送广播以重新扫描设备,但使用以下代码段仍然没有成功。

 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 File f = new File(absPathUri.getPath());
 Uri contentUri = Uri.fromFile(f);
 mediaScanIntent.setData(contentUri);
 this.sendBroadcast(mediaScanIntent);

Uses Below snippet to generate absPathUri

使用下面的代码片段生成 absPathUri

 if (Environment.MEDIA_MOUNTED.equals(state)) 
    {
        imageDirectory = new File(AppConstants.IMAGE_DIRECTORY_PATH);
    }
    else
    {
        imageDirectory = new File(AppConstants.IMAGE_DIRECTORY_PATH_DATA);
    }

    imageDirectory.mkdirs();
    File tempFile = new File(imageDirectory, getVideoName()+ jpg);
    Uri outputFileUri = Uri.fromFile( tempFile );
    absPathUri = outputFileUri;


  public static String getVideoName()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    try {
        name = sdf.format(new Date(System.currentTimeMillis()));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return name;
}

Below is the Layout which i used to show the ImageView in xml.

下面是我用来在 xml 中显示 ImageView 的布局。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
 >

<ImageView
    android:id="@+id/picView"
    android:layout_above="@+id/buttonPanel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="center"
    android:src="@drawable/accounts_glyph_password_default"
    />
<LinearLayout
    android:id="@id/buttonPanel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
     >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onDiscard"
         />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:text="@string/save"
        android:onClick="onSave"
        android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>

回答by Vikalp Patel

As suggested by gnuanu, setImageURI()is not better to use as reading and decoding on the UI thread, which can cause a latency hiccup.

正如 gnuanu 所建议的,setImageURI()在 UI 线程上用作读取和解码并不是更好,这可能会导致延迟打嗝。

Better to use the following:-

最好使用以下内容:-

setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Still these methods didn't solve my problem. As I was taking Picture with Camera and onClick of it sending to Next Activity which may cause latency hiccups.

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

So better to pass to another activity after some peroid of time . . just a sec is totally convient to get through it.

所以最好在一段时间后转移到另一个活动。. 只需一秒钟就可以通过它。

Snippet which solve my issue:-

解决我的问题的代码段:-

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

In Next Activity, catch the URI and rotate the Image as it would be in landscape mode.

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

回答by Nick

See here: ImageView setImageBitmap not working on certain devicesI found that in my case, I needed to call invalidate after setImageBitmap:

请参阅此处:ImageView setImageBitmap 在某些设备上不起作用我发现在我的情况下,我需要在 setImageBitmap 之后调用 invalidate:

imgView.setImageBitmap(bitmap);
imgView.invalidate();