【问题标题】:java.io.FileNotFoundException: content://com.android.contacts/contacts/24093/photo for old photos onlyjava.io.FileNotFoundException: content://com.android.contacts/contacts/24093/photo 仅适用于旧照片
【发布时间】:2014-04-24 10:16:23
【问题描述】:

我正在从我的手机联系人列表中获取我的联系人图片 uri。

但是,当我尝试将图像 uri 转换为位图时出现错误:

java.io.FileNotFoundException: content://com.android.contacts/contacts/24093/photo

它与我设备上的旧照片有关,好像我拍了一张新照片它显示正常。

所以可能是旧照片太大或者不行,但是异常说java.io.FileNotFoundException

我如何验证这是访问权限问题?

      final Bitmap bitmap;
      try {
        bitmap = MediaStore.Images.Media.getBitmap(ab.getContentResolver(), tryUri);
        ab.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            // make sure the tag is still the one we set at the beginning of this function
            if (toSet.getTag() == urlStr) {
              toSet.setImageBitmap(bitmap);
            }
          }
        });
      } catch (FileNotFoundException e) {
        String a = "1";
      } catch (IOException e) {
        String a = "1";
      } catch (Exception e) {
        String a = "1";
      }

【问题讨论】:

    标签: java android file bitmap


    【解决方案1】:

    两种可能性:1)您使用的 uri 不再好。您可以使用 ContactsContract 拉回一个新的。 2) 您要提取的联系人 24093 可能已不存在或已合并到另一个联系人中。因此不再有主要联系人照片。

    尝试使用 ContactsContract 而不是 getBitmap。如果照片不存在而不是 FileNotFound 错误,您最终会得到 null:

    private void showPic(String photoUri) {
        Bitmap[] array = new Bitmap[2];
        Cursor cursor = getContentResolver().query(Uri.parse(photoUri),
                new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
        try {
            if (cursor.moveToFirst()) {
                byte[] data = cursor.getBlob(0);
                if (data != null) {
                    array[0] = BitmapFactory.decodeStream(new ByteArrayInputStream(data));
                }
            }
        } finally {
            cursor.close();
        }
    
        if (array[0] != null) {toSet.setImageBitmap(array[0]);}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多