最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年05月05日 12:01:00   作者:十個(gè)雨點(diǎn)  
這篇文章主要為大家詳細(xì)介紹了android7.0實(shí)現(xiàn)分享圖片到朋友圈功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

此時(shí)就得使用其他的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 將需要分享到微信的圖片準(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ù)雜一點(diǎn),大概代碼如下(代碼功能是拍照的):

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中聲明這個(gè)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

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

相關(guān)文章

  • Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法

    Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法,涉及Android針對(duì)APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下
    2016-08-08
  • Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳

    Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Android簡易音樂播放器實(shí)現(xiàn)代碼

    Android簡易音樂播放器實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android簡易音樂播放器的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 基于Flutter制作一個(gè)火箭發(fā)射動(dòng)畫

    基于Flutter制作一個(gè)火箭發(fā)射動(dòng)畫

    北京時(shí)間10月16日0時(shí)23分,神舟十三號(hào)飛船成功發(fā)射,為慶祝這一喜事,本文將用Flutter制作一個(gè)火箭發(fā)射動(dòng)畫,感興趣的小伙伴可以動(dòng)手試一試
    2022-03-03
  • android自定義帶箭頭對(duì)話框

    android自定義帶箭頭對(duì)話框

    這篇文章主要為大家詳細(xì)介紹了android自定義帶箭頭對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Android Crash與ANR詳細(xì)介紹

    Android Crash與ANR詳細(xì)介紹

    對(duì)于Android開發(fā)的人來說,想必對(duì)Crash和ANR這倆都不陌生,并且都對(duì)其恨之入骨,因?yàn)樗鼈z的產(chǎn)生會(huì)大大影響用戶體驗(yàn)。所以,在此,結(jié)合本人的開發(fā)經(jīng)驗(yàn),對(duì)其做個(gè)總結(jié)
    2022-11-11
  • 利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載詳解

    利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載詳解

    這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實(shí)現(xiàn)簡單的異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • Android入門之讀寫本地文件的實(shí)現(xiàn)

    Android入門之讀寫本地文件的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)讀寫本地文件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下
    2022-12-12
  • 通過實(shí)例解析android Activity啟動(dòng)過程

    通過實(shí)例解析android Activity啟動(dòng)過程

    這篇文章主要介紹了通過實(shí)例解析android Activity啟動(dòng)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Android性能優(yōu)化全局異常處理詳情

    Android性能優(yōu)化全局異常處理詳情

    這篇文章主要介紹了Android性能優(yōu)化全局異常處理詳情,文章圍繞主題展開詳細(xì)的內(nèi)容協(xié)商,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-08-08

最新評(píng)論

从化市| 疏附县| 永城市| 满洲里市| 伊金霍洛旗| 昌乐县| 江华| 华坪县| 临沧市| 外汇| 唐河县| 西和县| 建宁县| 名山县| 织金县| 鄂伦春自治旗| 武穴市| 楚雄市| 紫云| 白朗县| 高碑店市| 葫芦岛市| 平乡县| 德令哈市| 车致| 霍州市| 班玛县| 凤冈县| 分宜县| 贡山| 封丘县| 寿阳县| 武鸣县| 鹰潭市| 吐鲁番市| 全南县| 安龙县| 中卫市| 武平县| 定边县| 卢湾区|