5

Tôi đang cố gắng tải ảnh Liên hệ với URI "content: //com.android.contacts/contacts/295" bằng cách sử dụng Glide.Cách tải URI bằng tiền tố "content: //" bằng Glide Android?

Khi tôi sử dụng

Glide.with(context).load(Uri.parse(contactPhoto).into(imageview) 

Glide mang lại cho tôi một FileNotFoundException

java.io.FileNotFoundException: File does not exist; URI: content://com.android.contacts/contacts/264, calling user: android.uid.shared:10006, calling package is one of: [com.android.providers.contacts, com.android.contacts, com.android.providers.userdictionary] 
     at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146) 
     at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:689) 
     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1080) 
     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:921) 
     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:848) 
     at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:21) 
     at com.bumptech.glide.load.data.FileDescriptorLocalUriFetcher.loadResource(FileDescriptorLocalUriFetcher.java:14) 
     at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44) 
     at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:83) 
     at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:53) 
     at com.bumptech.glide.load.engine.DecodeJob.decodeSource(DecodeJob.java:170) 
     at com.bumptech.glide.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:128) 
     at com.bumptech.glide.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122) 
     at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:101) 
     at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 
     at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:52) 

Rõ ràng Glide cố gắng để có được những hình ảnh từ các địa điểm sai.

Tôi sẽ đánh giá cao nếu ai đó chỉ cho tôi cách tải ảnh bằng "URI" nội dung: // ".

+0

https://github.com/bumptech/glide/issues/394 – TWiStErRob

Trả lời

0

Bạn cần sử dụng ContentResolver cho việc này.

ContentResolver contextResolver = context.getContentResolver(); 
Uri uri = Uri.parse("content://com.android.contacts/contacts/295"); 
Bitmap thumbnail = null; 
Cursor cursor = contentResolver.query(uri, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null); 

try { 
    if (cursor.moveToFirst()) { 
     final byte[] thumbnailBytes = cursor.getBlob(0); 
     if (thumbnailBytes != null) { 
      thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes, 0, thumbnailBytes.length); 
     } 
    } 
} 
finally { 
    cursor.close(); 
} 

if (thumbnail != null) { 
    imageView.setImageBitmap(thumbnail); 
} 

Hãy thử điều này. Điều này sẽ làm việc.

+0

Vâng tôi biết rằng có một phương thức được gọi là openContactPhotoInputStream. Nhưng điều đó sẽ truy xuất ảnh trong chuỗi chính và tôi đang gọi mã Glide trong Bộ điều hợp và muốn nó xử lý việc tải Hình ảnh trong chuỗi nền. –

+0

Tôi đã không sử dụng phương pháp đó vì nó không hoạt động dưới API 14. Và tôi không thấy vấn đề ở đây; Glide không thể nhanh hơn các lớp khung gốc. Bên cạnh đó, Glide không đảm bảo tự động xử lý bất kỳ 'Uri' nào. –

+0

Tôi nhận được 'java.lang.IllegalArgumentException: Dữ liệu cột không hợp lệ15' có liên quan đến' chuỗi tĩnh cuối cùng công khai PHOTO = DATA15'. Sử dụng API 21. –

1

Dường như Glide không xử lý ảnh nội dung Uri tự động.

Vì vậy, tôi đã giải quyết vấn đề của mình bằng cách sử dụng phương pháp RxJava.

Đây là một phương pháp mà phát ra một bitmap (Xin chú ý đến lịch trình như điều quan trọng là không bị tụt hiệu suất di chuyển)

private Observable<Bitmap> _getConvertInputStreamToBitmapObservable(ContentResolver cr, 
                    Uri contactUri) { 
    return Observable.create(new Observable.OnSubscribe<Bitmap>() { 
     @Override 
     public void call(Subscriber<? super Bitmap> subscriber) { 
      InputStream inputStream = 
        ContactsContract.Contacts.openContactPhotoInputStream(cr, contactUri); 
      if (inputStream != null) { 
       Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       subscriber.onNext(bitmap); 
      } 
      subscriber.onCompleted(); 
     } 
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); 
} 

Và đây là mã khách hàng sử dụng phương pháp này (Xin chú ý đến việc hủy đăng ký vì nó rất quan trọng trong việc tái chế).

 if (holder.bitmapSubscription != null) { 
      holder.bitmapSubscription.unsubscribe(); 
     } 

     holder.bitmapSubscription = _getConvertInputStreamToBitmapObservable(context.getContentResolver(), 
       contactUri) 
       .subscribe(holder.userImg::setImageBitmap); 
2

Bạn cần tạo trình tải tùy chỉnh sử dụng Trình phân giải nội dung. Trong Picasso chẳng hạn, công trình này có tác dụng vì có already a custom request handler sử dụng Trình phân giải nội dung.

Tôi đã tạo một tùy chỉnh Glide loader for contacts for my internal use mà bạn có thể thực hiện làm tham chiếu.

+2

Cảm ơn câu trả lời của bạn, tôi đã gửi yêu cầu kéo tới Glide thực hiện tính năng này một cách tự nhiên, tôi nghĩ rằng nó sẽ sớm được hợp nhất. https://github.com/bumptech/glide/pull/1119 –

+0

@ AhmedI.Khalil Bạn có thể đăng một ví dụ không? – Mussa

+2

https://github.com/bumptech/glide/issues/394 Yêu cầu tính năng hiện đã được giải quyết và nó sẽ được phát hành với v3.8.0 trong Glide. Bạn sẽ phải đợi v3.8.0 hoặc bạn có thể bao gồm mã nguồn Glide (branch v3.0) như một phụ thuộc trong dự án của bạn và xem xét ví dụ mẫu này mà tôi đã viết. https://github.com/bumptech/glide/blob/3.0/samples/contacturi/src/main/java/com/bumptech/glide/samples/contacturi/MainActivity.java –

-1
Uri uri = 
    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new 
    Long(contactsPojo.getId())); 
      final Uri displayPhotoUri = Uri.withAppendedPath(uri, 
        ContactsContract.Contacts.Photo.DISPLAY_PHOTO); 
      Glide.with(context) 
        .load(displayPhotoUri) 
        .placeholder(R.mipmap.ic_launcher) 
        .error(R.mipmap.ic_launcher) 
        .fallback(R.mipmap.ic_launcher) 
        .diskCacheStrategy(DiskCacheStrategy.ALL) 
        .into(holder.image); 
Các vấn đề liên quan