如何在Android中验证图像URI是否有效?

时间:2011-08-12 04:10:19

标签: android validation uri

我正在建立自己的联系人选择器,因为我需要多选支持。一切都运行正常,除了联系人图片的一个小问题。

对于没有图像的联系人,我显示的是“无图像”图像。这适用于手机通讯录中的联系人。我遇到了一个问题,但是当涉及到谷歌联系人的图片时。

我的大部分谷歌联系人都没有照片。但是,当我在“联系人”数据库​​中查询照片时,它仍会返回content://com.android.contacts/contacts/657/photo形式的URI(与 的联系人的格式相同强>有照片。

然后当我尝试将照片分配给QuickContactBadge时,使用bdg.setImageURI(pic);将其设置为基本上空白的图片,并记录一条静默的INFO消息,说明:

INFO/System.out(3968): resolveUri failed on bad bitmap uri: 
content://com.android.contacts/contacts/657/photo

我需要知道如何才能 a)验证URI
b)抓住上面的INFO消息 c)查询imageview /徽章以查看是否找到了有效图片

这样我就可以将这些联系人分配给我的“无图像”图像 我怎么能这样做呢?

编辑20110812.0044

根据劳伦斯的建议(我已删除),我尝试将其添加到我的代码中:

// rv is my URI variable
if(rv != null) {
    Drawable d = Drawable.createFromPath(rv.toString());
    if (d == null) rv = null;
}

虽然谷歌联系人现在得到我的“无图像”图像,但所有其他联系人也是如此,包括那些实际上有图像的人。

5 个答案:

答案 0 :(得分:12)

好的,我在浏览ImageView源代码后想出了如何做到这一点。它实际上是使用QuickContactBadge自己的方法,但如果有必要,可以始终提取the relevant code from the Badge/ImageView control here

在设置QCB的图像后,我检查它的drawable是否为null,而不是自己尝试(根据Laurence的建议)。这样做效果更好,因为ImageView小部件实际上有大量的检查代码。

这是我的最终代码:

bdg.setImageURI(pic);
if(bdg.getDrawable() == null) bdg.setImageResource(R.drawable.contactg);

这完全像我希望和期待的那样完美。

答案 1 :(得分:3)

回答关于如何检查MediaStore中的(数据)值的问题:

ContentResolver cr = getContentResolver();
String[] projection = {MediaStore.MediaColumns.DATA}
Cursor cur = cr.query(Uri.parse(contentUri), projection, null, null, null);
if(cur != null) {
    cur.moveToFirst();
    String filePath = cur.getString(0);

    if (filePath == null || filePath.isEmpty()) {
        // data not set
    } else if((new File(filePath)).exists()){
        // do something if it exists
    } else {
        // File was not found
        // this is binary data
    }
} else {
    // content Uri was invalid or some other error occurred 
}

灵感来自:https://stackoverflow.com/a/7649784/621690和其他人。

还可以检查列SIZEhttp://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#SIZE 如果没有数据值,它听起来应该包含0。但如果数据是文件路径,我不知道它包含什么。

答案 2 :(得分:2)

可能是未下载图像。我遇到了与whatsapp图像类似的问题。 一种方法可以如下:

Move-Item

答案 3 :(得分:0)

基于代码(http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/ImageView.java)我的检查Uri的解决方案:

public static Uri checkUriExists (Context  mContext,Uri mUri) {
    Drawable d = null;
    if (mUri != null) {
        if ("content".equals(mUri.getScheme())) {
            try {
                d = Drawable.createFromStream(
                        mContext.getContentResolver().openInputStream(mUri),
                        null);
            } catch (Exception e) {
                Log.w("checkUriExists", "Unable to open content: " + mUri, e);
                mUri = null;
            }
        } else {
            d = Drawable.createFromPath(mUri.toString());
        }

        if (d == null) {
            // Invalid uri
            mUri = null;
        }
    }

    return mUri;
}

答案 4 :(得分:-1)

我将此代码用于具有file:// authority

的Uri
Uri resimUri = Uri.parse(path_str);
File imgFile = new File(resimUri.getPath());
if (imgFile.exists()) {
    // file exists
}else {
    // file is not there
}