【问题标题】:Image is not setting in ImageView with setImageURI()图像未使用 setImageURI() 在 ImageView 中设置
【发布时间】:2014-01-16 09:39:46
【问题描述】:

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

我在 Next Screen 中有 ImageView,我用它来设置图像

imgView.setImageURI(absPathUri);

检查absPathUri 不为空。 ImagView 仍然是空白,没有图片。在调试模式下执行相同操作会将图片带入 ImageView

谁能告诉我哪里做错了?

同样发送广播重新扫描设备,但跟随 sn-p 仍然没有成功。

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

使用下面的sn-p生成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>

【问题讨论】:

  • 你能告诉我你是如何创建对象 absPathUri
  • @gnuanu :根据您的要求编辑了 OP。但是为什么它在调试模式下工作。
  • 根据文档,不建议使用 setImageURI 读取大图像,因为所有处理都发生在 UI 线程(读取/解码/渲染)developer.android.com/reference/android/widget/…
  • @gnuanu Bitmap bm=null; bm = BitmapFactory.decodeFile(absPathUri.getPath()); imgView.setImageBitmap(bm); 尝试了这些仍然没有图像显示

标签: android uri android-imageview android-image


【解决方案1】:

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

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

【讨论】:

  • setImageBitmap() 已弃用且 setImageUri() 未显示我的图像,请在这种情况下提供帮助。
【解决方案2】:

请看这里:ImageView setImageBitmap not working on certain devices 我发现在我的情况下,我需要在 setImageBitmap 之后调用 invalidate:

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

【讨论】:

    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2018-12-10
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多