Android 用原生分享多张图片适配 7.0、8.0

作者:HannyYeung
链接:https://www.jianshu.com/p/c9f071c51eba
声明:本文是 HannyYeung 原创。转载请联系作者获得授权。

常规操作只能适配6.0及以下

直接上分享的代码:

   Intent share_intent = new Intent();
       ArrayList<Uri> imageUris = new ArrayList<Uri>();
       Uri uri;
       for (File f : files) {
               imageUris.add(Uri.fromFile(f));
       }
       share_intent.setAction(Intent.ACTION_SEND_MULTIPLE);//设置分享行为
       share_intent.setType("image/png");//设置分享内容的类型
       share_intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
       context.startActivity(Intent.createChooser(share_intent, "Share"));

这样的话在 Android6.0 及以下都是没问题的

image.png

但是在 7.0、8.0 上就会出现出现:**android.os.FileUriExposedException **异常,出现这样的问题是因为分享限制原因,需要配置一些东西。

适配 Android 7.0、8.0

  • 代码书写

public static void originalShareImage(Context context, ArrayList<File> files) {
   Intent share_intent = new Intent();
   ArrayList<Uri> imageUris = new ArrayList<Uri>();
   Uri uri;
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       for (File f : files) {
           Uri imageContentUri =
FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider",f);
           imageUris.add(imageContentUri);
       }
   } else {
       for (File f : files) {
           imageUris.add(Uri.fromFile(f));
       }
   }
   share_intent.setAction(Intent.ACTION_SEND_MULTIPLE);//设置分享行为
   share_intent.setType("image/png");//设置分享内容的类型
   share_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   share_intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
   context.startActivity(Intent.createChooser(share_intent, "Share"));
}
  • Mainifest配置

<provider
   android:name="android.support.v4.content.FileProvider"
   android:authorities="your packge name.fileprovider"
   android:exported="false"
   android:grantUriPermissions="true">

   <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/file_paths">
</meta-data>
</provider>
  • file_paths配置

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <external-path
       name="myFile"
       path="folder">
</external-path>
</paths>

其中myFile可以随意写,folder是根目录下的文件夹,也就是你放图片的主目录

以上配置,可能出现微信分享不成功,QQ 可以分享成功,系统其它一些应用可以成功的情况,所以也不是很完善,所以,就想出了一个终极的解决办法

终极分享

public static void originalShareImage(Context context, ArrayList<File> files) {
   Intent share_intent = new Intent();
   ArrayList<Uri> imageUris = new ArrayList<Uri>();
   Uri uri;
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       for (File f : files) {
           Uri imageContentUri = getImageContentUri(context, f);
           imageUris.add(imageContentUri);
       }
   } else {
       for (File f : files) {
           imageUris.add(Uri.fromFile(f));
       }
   }
   share_intent.setAction(Intent.ACTION_SEND_MULTIPLE);//设置分享行为
   share_intent.setType("image/png");//设置分享内容的类型
   share_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
   share_intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
   context.startActivity(Intent.createChooser(share_intent, "Share"));
}
  • 获取图片的绝对的分享地址

/**
*
* @param context
* @param imageFile
* @return content 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);
   if (cursor != null && cursor.moveToFirst()) {
       int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
       Uri baseUri = Uri.parse("content://media/external/images/media");
       return Uri.withAppendedPath(baseUri, "" + id);
   } else {
       if (imageFile.exists()) {
           ContentValues values = new ContentValues();
           values.put(MediaStore.Images.Media.DATA, filePath);
           return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
       } else {
           return null;
       }
   }
}

按照以上步骤操作,可能会解决分享的问题,亲测有效!


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值