温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决

Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决

发布时间:2020-11-03 19:29:53 来源:亿速云 阅读:1198 作者:Leah 栏目:开发技术

本篇文章为大家展示了Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

 private void systemShareWeChat(int shareTag,String photoPath){
    Resources res=getResources();
    Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.share);
    File f = null;

    ComponentName comp1,comp;
    comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//调用系统分享给微信朋友
    comp1 = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//调用系统分享给微信朋友圈

    try {
    //将Android中drawable图片保存到本地
      String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"share"+".jpg";
       f = new File(dir);
      if (!f.exists()) {
        f.getParentFile().mkdirs();
        f.createNewFile();
      }
      FileOutputStream out = new FileOutputStream(f);
      bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
      out.flush();
      out.close();
      Uri uri = FileProvider.getUriForFile(NativePhoto.this.getApplicationContext(),
          "com.lipuwulian.blesample.provider", f);//这个是版本大于Android7.0(包含)临时访问文件,没有这个会报异常
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace(); }

    ArrayList<Uri> imageUris = new ArrayList<Uri>();
    imageUris.add(UrigetImageContentUri(NativePhoto.this,new File(photoPath)));//这个是分享本地存储的图片
    imageUris.add(UrigetImageContentUri(NativePhoto.this,f));
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    if(shareTag==0){
      shareIntent.setComponent(comp1);//分享给微信朋友圈
    }else if(shareTag==1){
      shareIntent.setComponent(comp);//分享给微信朋友
    }
    //如果去掉shareIntent.setComponent("*");系统会调出所有的分享软件
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
    shareIntent.setType("image/*");
    startActivity(shareIntent);

  }

//如果是微信分享的话一定一定将这个直接复制到自己项目中,将自己图片路径换为content:不然就会出现上述错误

public static Uri UrigetImageContentUri(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 +"=&#63; ", 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;

  }

这样就解决了调用系统分享报出分享失败,分享多文件必须为图片格式的错误了。

在AndroidManifest中临时文件注册解决Android7.0版本及其之后文件uri报错问题

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name1"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--    //临时访问文件的注册-->
    <provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="com.lipuwulian.blesample.provider"//这是自己的包名加“.provider”
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
    </provider>

    <activity android:name="com.lipuwulian.blesample.MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
   
  </application>

在res文件夹下创建xml文件夹、

Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决

file_paths文件里的内容:path是data/包名加.testapplication/

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<resources>
  <paths>
    <external-path
      name="files_root"
      path="Android/data/com.lipuwulian.blesample.testapplication/" />
    <external-path
      name="external_storage_root"
      path="." />
  </paths>
</resources>

上述内容就是Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI