国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android7.0實現分享圖片到朋友圈功能

2019-10-22 18:12:00
字體:
來源:轉載
供稿:網友

本文實例為大家分享了android實現分享圖片朋友圈功能的具體代碼,供大家參考,具體內容如下

在Android7.0中,系統對scheme為file://的uri進行了限制,所以通過這種uri來進行分享的一些接口就不能用了,比如使用代碼來調用分享朋友圈的接口。

此時就得使用其他的URI scheme來代替 file://,比如MediaStore的 content://。直接上代碼:

private static boolean checkInstallation(Context context, String packageName) {  try {   context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);   return true;  } catch (PackageManager.NameNotFoundException e) {   return false;  } } public static void shareToWeChat(View view, Context context) {  // TODO: 2015/12/13 將需要分享到微信的圖片準備好  try {   if (!checkInstallation(context, "com.tencent.mm")) {    SnackBarUtil.show(view, R.string.share_no_wechat);    return;   }   Intent intent = new Intent();   //分享精確到微信的頁面,朋友圈頁面,或者選擇好友分享頁面   ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");   intent.setComponent(comp);   intent.setAction(Intent.ACTION_SEND_MULTIPLE);   intent.setType("image/*");//  intent.setType("text/plain");   //添加Uri圖片地址//  String msg=String.format(getString(R.string.share_content), getString(R.string.app_name), getLatestWeekStatistics() + "");   String msg = context.getString(R.string.share_content);   intent.putExtra("Kdescription", msg);   ArrayList<Uri> imageUris = new ArrayList<Uri>();   // TODO: 2016/3/8 根據不同圖片來設置分享   File dir = context.getExternalFilesDir(null);   if (dir == null || dir.getAbsolutePath().equals("")) {    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());   }   File pic = new File(dir, "bigbang.jpg");   pic.deleteOnExit();   BitmapDrawable bitmapDrawable;   if (Build.VERSION.SDK_INT < 22) {    bitmapDrawable = (BitmapDrawable) context.getResources().getDrawable(R.mipmap.bannar);   } else {    bitmapDrawable = (BitmapDrawable) context.getDrawable(R.mipmap.bannar);   }   try {    bitmapDrawable.getBitmap().compress(Bitmap.CompressFormat.JPEG, 75, new FileOutputStream(pic));   } catch (FileNotFoundException e) {    e.printStackTrace();   }   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {    imageUris.add(Uri.fromFile(pic));   }else {    //修復微信在7.0崩潰的問題    Uri uri =Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(), pic.getAbsolutePath(), "bigbang.jpg", null));    imageUris.add(uri);   }   intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);   ((Activity) context).startActivityForResult(intent, 1000);  }catch (Throwable e){   SnackBarUtil.show(view,R.string.share_error); }

還有一種方式,就是FileProvider來分享文件,操作起來稍微復雜一點,大概代碼如下(代碼功能是拍照的):

String mCurrentPhotoPath;private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile(  imageFileName, /* prefix */  ".jpg",   /* suffix */  storageDir  /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image;}static final int REQUEST_TAKE_PHOTO = 1;private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) {  // Create the File where the photo should go  File photoFile = null;  try {   photoFile = createImageFile();  } catch (IOException ex) {   // Error occurred while creating the File   ...  }  // Continue only if the File was successfully created  if (photoFile != null) {   Uri photoURI = FileProvider.getUriForFile(this,             "com.example.android.fileprovider",             photoFile);   takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);   startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);  } }}

還要在manifest中聲明這個FileProvider

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

在res/xml/文件夾下新建文件file_paths.xml:

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /></paths>

參考:stackoverflow

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 突泉县| 湖州市| 黑山县| 七台河市| 珲春市| 安顺市| 兰西县| 泾阳县| 朔州市| 焦作市| 巴塘县| 呼图壁县| 遂川县| 中西区| 永寿县| 公安县| 桐城市| 青浦区| 玛纳斯县| 茶陵县| 克什克腾旗| 沈阳市| 北碚区| 广平县| 梁山县| 高雄市| 井研县| 北辰区| 东海县| 绥化市| 兴隆县| 名山县| 内丘县| 仁化县| 玉田县| 合山市| 咸阳市| 巴林右旗| 吉林省| 收藏| 收藏|