Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决(android,roi,开发技术)

时间:2024-05-04 11:20:09 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :
 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>
 <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Android调用系统分享给微信,出现分享失败,分享多文件必须为图片格式问题如何解决的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:nginx中怎么设置禁止解析php文件下一篇:

15 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18