本文实例为大家分享了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
".jpg",
storageDir
);
// 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
......
在res/xml/文件夹下新建文件file_paths.xml:
参考:stackoverflow
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



