简体   繁体   English

java.io.FileNotFoundException:content://com.android.contacts/contacts/24093/photo仅适用于旧照片

[英]java.io.FileNotFoundException: content://com.android.contacts/contacts/24093/photo for old photos only

I'm getting my contacts image uris from my phone contacts list. 我正在从电话联系人列表中获取联系人图像uri。

However when i try convert the image uri to bitmap i get an error: 但是,当我尝试将图像uri转换为位图时,出现错误:

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

It has something to do with old photos on my device, as if i take a new photo it is shown OK. 这与设备上的旧照片有关,好像我拍了一张新照片一样。

so maybe the old photos are too big or not OK, but the exception says java.io.FileNotFoundException 因此,也许旧照片太大或不合适,但是异常显示java.io.FileNotFoundException

How can i verify it's a access rights issue? 我如何确认这是访问权限问题?

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

Two possibilities: 1) The uri you're using is not good anymore. 两种可能性:1)您使用的uri不再好。 You can repull a new one using ContactsContract. 您可以使用ContactsContract重新拉动新的。 2) The contact you're pulling, 24093, may no longer exist or was merged into another contact. 2)您要拉的联系人24093可能不再存在或已合并到另一个联系人中。 Therefore there is no primary contact photo anymore. 因此,不再有主要联系人照片。

Try this using the ContactsContract instead of getBitmap. 使用ContactsContract而不是getBitmap尝试此操作。 You'll end up with a null if the photo doesn't exist instead of a FileNotFound error: 如果照片不存在,则将以null结尾,而不是FileNotFound错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM