图像不在的ImageView与setImageURI设置() [英] Image is not setting in ImageView with setImageURI()

查看:242
本文介绍了图像不在的ImageView与setImageURI设置()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关拍照创建自己的相机为I'need重点关注。相机工作正常完美的罚款。通过 URI 活动之间。

我有<大骨节病>的ImageView 我在其中可以用来设置

图片下一屏幕

  imgView.setImageURI(absPathUri);

经过了 absPathUri 不为空。尽管如此<大骨节病> ImagView 是空白的,没有图片。这样做在调试模式下带来相同的图片<大骨节病>的ImageView

大家能否请你告诉我哪里做错了吗?

同时发送广播重新扫描设备,但仍与下面的代码段没有成功。

 意图mediaScanIntent =新意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 文件f =新的文件(absPathUri.getPath());
 乌里contentUri = Uri.fromFile(F);
 mediaScanIntent.setData(contentUri);
 this.sendBroadcast(mediaScanIntent);

使用下面代码段生成 absPathUri

 如果(Environment.MEDIA_MOUNTED.equals(州))
    {
        imageDirectory =新的文件(AppConstants.IMAGE_DIRECTORY_PATH);
    }
    其他
    {
        imageDirectory =新的文件(AppConstants.IMAGE_DIRECTORY_PATH_DATA);
    }    imageDirectory.mkdirs();
    文件临时文件=新的文件(imageDirectory,getVideoName()+ JPG格式);
    乌里outputFileUri = Uri.fromFile(临​​时文件);
    absPathUri = outputFileUri;
  公共静态字符串getVideoName()
{
SimpleDateFormat的SDF =新的SimpleDateFormat(YYYYMMDDHHMMSS);
    尝试{
        名称= sdf.format(新的Date(System.currentTimeMillis的()));
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    返回名称;
}

下面是我用来显示XML的ImageView的布局。

 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:背景=@绘制/ BG
 &GT;&LT; ImageView的
    机器人:ID =@ + ID / picView
    机器人:layout_above =@ + ID / buttonPanel
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:scaleType =中心
    机器人:SRC =@绘制/ accounts_glyph_password_default
    /&GT;
&LT;的LinearLayout
    机器人:ID =@ ID / buttonPanel
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:方向=横向
     &GT;    &LT;按钮
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        安卓的onClick =onDiscard
         /&GT;    &LT;按钮
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =10dip
        机器人:文字=@字符串/保存
        安卓的onClick =的onSave
        机器人:文字颜色=#FFFFFF/&GT;
&LT; / LinearLayout中&GT;
&LT; / RelativeLayout的&GT;


解决方案

作为建议的gnuanu, setImageURI()是不是更好的读取和解码的UI使用线程,这可能导致一个延迟打嗝。

最好使用下列内容: -

setImageDrawable(android.graphics.drawable.Drawable)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory来代替。

不过这些方法并没有解决我的问题。当我正在用相机和它的onClick发送给下一个活动可能导致延迟打嗝的图片。

因此​​,更好的某个时间peroid后传递到另一个活动。 。只是几秒钟是完全通过无限便捷它得到。

摘录其解决我的问题: -

 最终意图picIntent =新意图(CameraActivity.this,PicSaveActivity.class);
picIntent.putExtra(URI,pathUri.toString());
处理程序处理程序=新的处理程序()
{
公共无效的handleMessage(消息MSG){
    startActivityForResult(picIntent,PICTAKEN);
    };
};
 消息味精= handler.obtainMessage();
 handler.sendMessageDelayed(MSG,1000);

在接下来的活动,捕捉URI和旋转图像,因为它会在横向模式。

 如果(absPathUri!= NULL)
{
    位图myImg = BitmapFactory.de codeFILE(absPathUri.getPath());
    字模=新的Matrix();
    matrix.postRotate(90);
    位图旋转= Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),
            矩阵,真);
    imgView.setImageBitmap(旋转);
    }

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

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.

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

 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.

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

解决方案

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

Better to use the following:-

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.

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

这篇关于图像不在的ImageView与setImageURI设置()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆