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

2024-03-26

创建自己的相机,因为我需要对焦来拍照。相机工作正常,正如预期的那样。通过URI活动之间。

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

imgView.setImageURI(absPathUri);

Checked that absPathUri is not null. Still ImagView is blank with no pictures. Doing the same in debug mode brings the Picture in ImageView.

有人可以告诉我哪里做错了吗?

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

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

使用下面的代码片段来生成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;
}

下面是我用来在 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>

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

最好使用以下内容:-

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

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

因此,最好在一段时间后转到另一项活动。 。只需一秒钟就可以完成它,非常方便。

解决我的问题的片段:-

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

在下一个活动中,捕获 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);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未使用 setImageURI() 在 ImageView 中设置图像 的相关文章

随机推荐