6

Tôi có một ứng dụng sử dụng Universal Image Loader để tải ảnh xuống từ internet, lưu chúng vào dữ liệu/dữ liệu/com.myapp/cache và hiển thị trong ImageViews.Chia sẻ hình ảnh được lưu trong bộ nhớ cache bằng FileProvider

Tôi cũng muốn thêm chia sẻ (WhatsApp, Facebook, Instagram, Dropbox vv) để ứng dụng của tôi, vì vậy tôi cố gắng sử dụng mã này:

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_STREAM, photoUri); 
intent.setType("image/jpeg"); 
startActivity(Intent.createChooser(intent, "Share image")); 

photoUri là uri trong thư mục bộ nhớ cache mà các ứng dụng khác không có quyền đọc.

Vì vậy, tôi googled/stackoverflowed và found out mà tôi cần phải sử dụng FileProvider.

Tôi đã cấu hình FileProvider như sau (as was written here) trong manifest của tôi:

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="com.myapp.fileprovider" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths" /> 
</provider> 

Và file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <cache-path name="photo_cache" path="/"/> 
</paths> 

Sau đó, tôi sử dụng đoạn mã sau trong hoạt động của tôi trong nút OnClickListener:

File photoFile = ImageLoader.getInstance().getDiscCache().get("HTTP LINK HERE"); 
// returns File of "/data/data/com.myapp/cache/-1301123243" 
Uri photoUri = FileProvider.getUriForFile(MyActivity.this, "com.myapp.fileprovider", photoFile); 
// returns Uri of "content://com.myapp.fileprovider/photo_cache/-1301123243" 
photoUri = Uri.parse(photoUri.toString() + ".jpg"); 
// then I add .jpg to file name 

// Create intent 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_STREAM, photoUri); 
intent.setType("image/jpeg"); 

// Grant permissions to all apps that can handle this intent 
// thanks to this answer http://stackoverflow.com/a/18332000 
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 
for (ResolveInfo resolveInfo : resInfoList) { 
    String packageName = resolveInfo.activityInfo.packageName; 
    grantUriPermission(packageName, photoUri, 
     Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); 
} 

// And start 
startActivity(Intent.createChooser(intent, "Share image")); 

Tuy nhiên, tùy thuộc vào ứng dụng tôi nhận được ngoại lệ lạ tions. Như thế này:

1974-1974/com.whatsapp W/Bundle﹕ Key android.intent.extra.STREAM expected ArrayList but value was a android.net.Uri$HierarchicalUri. The default value <null> was returned. 
1974-1974/com.whatsapp W/Bundle﹕ Attempt to cast generated internal exception: 
java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.util.ArrayList 
     at android.os.Bundle.getParcelableArrayList(Bundle.java:1223) 
     at android.content.Intent.getParcelableArrayListExtra(Intent.java:4425) 
     at com.whatsapp.ContactPicker.d(ContactPicker.java:320) 
     at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:306) 
     at android.app.Activity.performCreate(Activity.java:5104) 

Hoặc Tôi chỉ thấy các bản ghi như thế này:

591-963/system_process W/ActivityManager﹕ Permission denied: checkComponentPermission() 

Và tất nhiên các ứng dụng bản thân cho tôi nâng cốc chúc mừng với các lỗi (mà họ không thể mở hoặc tải file).

Tôi đã thử, googled, stackoverflowed A LOT, vì vậy bây giờ tôi đang đăng ở đây. Tôi đoán tôi đang làm đúng, có thể chỉ là một số điều nhỏ là sai ...

Bất kỳ trợ giúp nào sẽ được đánh giá cao!

+0

bạn có thể đặt phép internet trong file xml android – Riskhan

+0

@kTekkie Tôi không nghĩ rằng bạn đã hiểu đúng câu hỏi, xin lỗi – Makks129

+0

@ Makks129 bạn đang tìm nạp hình ảnh bằng JSON, nếu có, bạn có thể chia sẻ mã mẫu với tôi không hoặc có simillar i fork trên github, yêu cầu của tôi là: Universal Image Loader + với JSON trong GridView ---- không muốn sử dụng liên kết liên tục cho hình ảnh – Sun

Trả lời

0

Tôi nghĩ rằng đây có thể là vấn đề với cơ sở hạ tầng FileProvider hoặc ứng dụng bạn đang chia sẻ.

Tôi đã cố gắng sử dụng hỗ trợ này để chia sẻ hình ảnh với các ứng dụng khác. Nó hoạt động tốt với Thư viện, Drive, Gmail và Keep. Không thành công với Ảnh và Google+.

Tôi thấy các mục nhập như sau trong LogCat dẫn tôi tin rằng Uri đang được chuyển từ Hoạt động này sang Hoạt động khác. Tuy nhiên, sự cho phép (tạm thời) được đặt trong Intent đang bị mất.

11-19 21: 14: 06,031: I/ActivityManager (433): com.google.android.apps.plus/.phone.HostPhotoViewIntentActivity Displayed: + 848ms

+0

Bạn đã thử phương pháp này với Instagram, Facebook hay Messenger? Nó sẽ được làm việc, nhưng nó không xuất hiện như vậy. –

Các vấn đề liên quan