1. 程式人生 > >android 7.0分享朋友圈提示:“獲取資源失敗,僅支援分享照片至朋友圈”或者FileProvider生成的Uri無法識別

android 7.0分享朋友圈提示:“獲取資源失敗,僅支援分享照片至朋友圈”或者FileProvider生成的Uri無法識別

需求是把網路圖片地址是string型別,生成圖片,分享到朋友圈,遇到的問題是,7.0之前沒有問題分享朋友圈,順便切上程式碼:

 Intent intent = new Intent();
        Uri uri = null;
        try {
            String authority = context.getPackageName() + ".FileProvider";
            ComponentName componentName = new ComponentName("com.tencent.mm",
                    type == 0 ? "com.tencent.mm.ui.tools.ShareImgUI" : "com.tencent.mm.ui.tools.ShareToTimeLineUI");
            intent.setComponent(componentName);
//插入多張圖片 ACTION_SEND_MULTIPLE

            intent.setAction(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra("Kdescription", text);

            //7.0以上需要新增臨時讀取許可權
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//插入多張圖片
//                ArrayList<Uri> imageUris = new ArrayList<Uri>();
                //插入系統相簿
//                Uri uri =Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(),null,null));
                uri = FileProvider.getUriForFile(context, authority, file);       
//                imageUris.add(uri);
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } else {
                uri = Uri.fromFile(file);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

看著應該差不多了。嗯,,再貼上一個寫的比較全的別的部落格包括qq分享等:

https://www.jianshu.com/p/9522e24713e1

以為完事了,可是還是沒法解決,還是提示“獲取資源失敗,僅支援分享照片至朋友圈”,各種百度,一直都沒有找到解決的方法,dug看uri也是有的,這就很奇怪了,有的說是微信訪問不到外部儲存目錄,也嘗試修改了儲存地址還是不行,還是怪思路不清晰,沒有找到根本方法,看到這個部落格就明白了。順便貼上地址:

Android7.0分享朋友圈FileProvider生成的Uri無法被識別

ε=(´ο`*)))唉,怎麼沒有想到fileprovider生成的uri地址,應用不能識別了,還真是沒想到,可能是經驗不足吧,要把uri地址轉換一下。

最後再貼上程式碼:

public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);
        Uri uri = null;

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                Uri baseUri = Uri.parse("content://media/external/images/media");
                uri = Uri.withAppendedPath(baseUri, "" + id);
            }

            cursor.close();
        }

        if (uri == null) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }

        return uri;
    }