android7.0实现分享图片到朋友圈功能
本文实例为大家分享了android实现分享图片到朋友圈功能的具体代码,供大家参考,具体内容如下
在Android7.0中,系统对scheme为file://的uri进行了限制,所以通过这种uri来进行分享的一些接口就不能用了,比如使用代码来调用分享朋友圈的接口。
此时就得使用其他的URIscheme来代替file://,比如MediaStore的content://。直接上代码:
privatestaticbooleancheckInstallation(Contextcontext,StringpackageName){
try{
context.getPackageManager().getPackageInfo(packageName,PackageManager.GET_ACTIVITIES);
returntrue;
}catch(PackageManager.NameNotFoundExceptione){
returnfalse;
}
}
publicstaticvoidshareToWeChat(Viewview,Contextcontext){
//TODO:2015/12/13将需要分享到微信的图片准备好
try{
if(!checkInstallation(context,"com.tencent.mm")){
SnackBarUtil.show(view,R.string.share_no_wechat);
return;
}
Intentintent=newIntent();
//分享精确到微信的页面,朋友圈页面,或者选择好友分享页面
ComponentNamecomp=newComponentName("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图片地址
//Stringmsg=String.format(getString(R.string.share_content),getString(R.string.app_name),getLatestWeekStatistics()+"");
Stringmsg=context.getString(R.string.share_content);
intent.putExtra("Kdescription",msg);
ArrayListimageUris=newArrayList();
//TODO:2016/3/8根据不同图片来设置分享
Filedir=context.getExternalFilesDir(null);
if(dir==null||dir.getAbsolutePath().equals("")){
dir=newFile(Environment.getExternalStorageDirectory().getAbsolutePath());
}
Filepic=newFile(dir,"bigbang.jpg");
pic.deleteOnExit();
BitmapDrawablebitmapDrawable;
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,newFileOutputStream(pic));
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
if(Build.VERSION.SDK_INT
还有一种方式,就是FileProvider来分享文件,操作起来稍微复杂一点,大概代码如下(代码功能是拍照的):
StringmCurrentPhotoPath;
privateFilecreateImageFile()throwsIOException{
//Createanimagefilename
StringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
StringimageFileName="JPEG_"+timeStamp+"_";
FilestorageDir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Fileimage=File.createTempFile(
imageFileName,/*prefix*/
".jpg",/*suffix*/
storageDir/*directory*/
);
//Saveafile:pathforusewithACTION_VIEWintents
mCurrentPhotoPath="file:"+image.getAbsolutePath();
returnimage;
}
staticfinalintREQUEST_TAKE_PHOTO=1;
privatevoiddispatchTakePictureIntent(){
IntenttakePictureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
//Ensurethatthere'sacameraactivitytohandletheintent
if(takePictureIntent.resolveActivity(getPackageManager())!=null){
//CreatetheFilewherethephotoshouldgo
FilephotoFile=null;
try{
photoFile=createImageFile();
}catch(IOExceptionex){
//ErroroccurredwhilecreatingtheFile
...
}
//ContinueonlyiftheFilewassuccessfullycreated
if(photoFile!=null){
UriphotoURI=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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。