android分享分享到朋友圈图片,android7.0 通过代码 分享图片到朋友圈

转载注明出处:简书-十个雨点

在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 imageUris = new ArrayList();

// 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

...

android:name="android.support.v4.content.FileProvider"

android:authorities="com.example.android.fileprovider"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths">

...

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

具体代码可以参考Bigbang项目的ShareCard的处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值