Android:setImageUri 失败 [英] Android: setImageUri failing

查看:26
本文介绍了Android:setImageUri 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个插入 Activity,它有一个图像选择器,其中的 Uri 存储为准备放入数据库的字符串.

My application has an insert activity that has an image picker and the Uri from that is stored as a string ready to put into a database.

public void onActivityResult(int reqCode, int resCode, Intent data) {
    if(resCode == RESULT_OK) {
        if(reqCode == 1) {
            imageUri = data.getData().toString();
        }
    }
}

imageUri 是稍后在按下按钮时放入数据库的字符串.

imageUri is the String that is then later put in a database on a button press.

一旦所有的东西都被插入到数据库中,活动完成返回主要活动,在 onResume 方法中,数据库/表中的所有数据都被检索到一个对象列表中,然后将这些数据放入用于形成我的列表视图的自定义数组适配器.

Once everything is inserted into the database the activity finishes to return the main activity which in the onResumemethod all the data from the database/table is retrieved in a List of objects that are then put into a custom array adapter to form my list view.

if(!uriString.equals("")) {
     image.setImageURI(Uri.parse(uriString));
}

这是在阵列适配器内设置图像的方式.奇怪的是,在插入活动关闭后,图像是可见的,但是一旦我打开另一个活动并返回图像是空白的.我收到错误

This is how the image is set inside the array adapter. The strange thing is that right after the insert activity closes the image is visible but as soon as I open another activity and return the image is blank. I get the error

resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/image%3A29938

我不知道为什么它无法解决,因为开始很好,但后来它不起作用.

I don't know why it can't be resolved because it is fine to start with but then it doesn't work.

推荐答案

内容 URI 需要正确解析,因为它们不像普通的 filePath,因此我们需要一些解析器.以下是如何处理此问题的示例:

Content URI's need to be parsed properly as they are not like normal filePath and hence we need some resolver. Here is an example how to approach this:

对代码的更改

if(!uriString.equals("")) {
     image.setImageURI(Uri.parse(getPathFromURI(<your app context>, Uri.parse("content://blah/blah/blah"))));
}

URI 解析器的方法定义.

Method definition for the URI parser.

public String getPathFromURI(Context context, Uri contentUri) {
   Cursor mediaCursor = null;
   try { 
           String[] dataPath = { MediaStore.Images.Media.DATA };
           mediaCursor = context.getContentResolver().query(contentUri,  dataPath, null, null, null);
           int column_index = mediaCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           mediaCursor.moveToFirst();
           return mediaCursor.getString(column_index);
   } finally {
       if (mediaCursor != null) {
             mediaCursor.close();
       }
   }
}

这篇关于Android:setImageUri 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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