激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - android7.0實現(xiàn)分享圖片到朋友圈功能

android7.0實現(xiàn)分享圖片到朋友圈功能

2022-02-12 17:20十個雨點 Android

這篇文章主要為大家詳細(xì)介紹了android7.0實現(xiàn)分享圖片到朋友圈功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 將需要分享到微信的圖片準(zhǔn)備好
  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 根據(jù)不同圖片來設(shè)置分享
   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 {
    //修復(fù)微信在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來分享文件,操作起來稍微復(fù)雜一點,大概代碼如下(代碼功能是拍照的):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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:

?
1
2
3
4
<?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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/l465659833/article/details/53469441

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级做受大片免费视频 | 国产一区二区三区色淫影院 | 国产精品爆操 | 国产精品久久久av | 99精彩视频在线观看 | 毛片在线播放视频 | 久久艹综合 | 久草中文网 | 久久成年人视频 | 亚洲一区二区网址 | www久| 久久久精品网 | 欧美久久久一区二区三区 | av免费在线观看免费 | 男女生羞羞视频网站在线观看 | 精品在线观看一区 | 色婷婷久久久久久 | 中文字幕在线第二页 | www.91pron| 日本成年网 | 美女很黄很黄免费的 | 久热久操 | 哪里可以看免费的av | 男女一边摸一边做羞羞视频免费 | 97超级碰碰人国产在线观看 | 日本人乱人乱亲乱色视频观看 | 成人区精品一区二区婷婷 | 久久久中精品2020中文 | 92看片淫黄大片一级 | 久久影院在线观看 | 国产精品999在线观看 | 国产九色91| 黄色片视频观看 | 性片久久| 国产成人77亚洲精品www | 激情午夜天 | 北条麻菲 | 被玩坏了的女老师(高h np) | 亚洲精品免费播放 | 亚洲小视频在线观看,com | 欧美一级黄色录像片 |