Android 未使用setImageURI()在ImageView中设置图像
创建自己的相机,因为我需要集中精力拍照。这架照相机工作得和预期的一样好。在活动之间传递Android 未使用setImageURI()在ImageView中设置图像,android,uri,android-imageview,android-image,Android,Uri,Android Imageview,Android Image,创建自己的相机,因为我需要集中精力拍照。这架照相机工作得和预期的一样好。在活动之间传递URI 我在下一个屏幕中使用ImageView设置图像 imgView.setImageURI(absPathUri); 已检查absPathUri是否不为空。静止图像视图是空白的,没有图片。在调试模式下执行相同操作会使图片显示在ImageView中 谁能告诉我哪里做错了? 同时发送广播以重新扫描设备,但仍无法成功执行以下代码段 Intent mediaScanIntent = new Intent(Int
URI
我在下一个屏幕中使用ImageView设置图像
imgView.setImageURI(absPathUri);
已检查absPathUri
是否不为空。静止图像视图是空白的,没有图片。在调试模式下执行相同操作会使图片显示在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)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory。
但这些方法并没有解决我的问题。当我用相机拍照时,点击一次就可以发送到下一个活动,这可能会导致延迟打嗝
因此,最好在一段时间后进行另一项活动。只要一秒钟就可以轻松度过难关
解决我的问题的代码片段:-
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);
}
请看这里:
我发现在我的例子中,我需要在setImageBitmap之后调用invalidate:
imgView.setImageBitmap(bitmap);
imgView.invalidate();
你能告诉我你是如何创建这个对象的吗absPathUri@gnuanu:按您的要求编辑OP。但是为什么它在调试模式下工作。根据文档,不建议使用setImageURI读取大图像,因为所有处理都发生在UI线程(读取/解码/渲染)@gnuanu
Bitmap bm=null;bm=BitmapFactory.decodeFile(absPathUri.getPath());imgView.setImageBitmap(bm)代码>是否尝试过这些仍然没有图像显示upsetImageBitmap()已弃用,setImageUri()未显示我的图像,请在此情况下提供帮助。