Android使用Intent一键分享图片文字到腾讯、新浪、开心、人人等
2016-11-01 14:32
716 查看
现在分享应用很多,消耗研发与维护时间,增加项目成本,而且集成分享功能后我们的应用包会比以前要大几MB。其实有android的Intent可以满足分享文字和图片的需求,而且不增加应用包的大小。
[java] view plaincopy
/**
* 判断是否安装腾讯、新浪等指定的分享应用
* @param packageName 应用的包名
*/
public boolean checkInstallation(String packageName){
try {
this.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
如果没有安装指定的应用,下面代码引导跳转去Google Play上某个应用的详细页面
[java] view plaincopy
Uri uri = Uri.parse("market://details?id=应用包名");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
用下面代码可以弹出一个分享列表让用户选择需要分享哪些文字内容。如果去调用setPackage方法的注销就会直接跳转新浪微博分享文字。
[java] view plaincopy
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.setPackage("com.sina.weibo");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "你好 ");
intent.putExtra(Intent.EXTRA_TITLE, "我是标题");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "请选择"));
分享单张图片,操作与分享文字类似。
[java] view plaincopy
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// startActivity(Intent.createChooser(shareIntent, "请选择"));
用Intent分享多张图片
[java] view plaincopy
/**
* 分享多张照片
*/
private void sendMultiple(){
Intent intent=new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, getUriListForImages());
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "你好 ");
intent.putExtra(Intent.EXTRA_TITLE, "我是标题");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "请选择"));
}
/**
* 设置需要分享的照片放入Uri类型的集合里
*/
private ArrayList<Uri> getUriListForImages() {
ArrayList<Uri> myList = new ArrayList<Uri>();
String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/DCIM/100ANDRO/";
File imageDirectory = new File(imageDirectoryPath);
String[] fileList = imageDirectory.list();
if(fileList.length != 0) {
for(int i=0; i<5; i++){
try{
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, fileList[i]);
values.put(Images.Media.DISPLAY_NAME, fileList[i]);
values.put(Images.Media.DATE_TAKEN, new Date().getTime());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode());
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileList[i]);
values.put("_data", imageDirectoryPath + fileList[i]);
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
myList.add(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return myList;
}
[java] view plaincopy
/**
* 判断是否安装腾讯、新浪等指定的分享应用
* @param packageName 应用的包名
*/
public boolean checkInstallation(String packageName){
try {
this.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
如果没有安装指定的应用,下面代码引导跳转去Google Play上某个应用的详细页面
[java] view plaincopy
Uri uri = Uri.parse("market://details?id=应用包名");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
用下面代码可以弹出一个分享列表让用户选择需要分享哪些文字内容。如果去调用setPackage方法的注销就会直接跳转新浪微博分享文字。
[java] view plaincopy
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.setPackage("com.sina.weibo");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "你好 ");
intent.putExtra(Intent.EXTRA_TITLE, "我是标题");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "请选择"));
分享单张图片,操作与分享文字类似。
[java] view plaincopy
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// startActivity(Intent.createChooser(shareIntent, "请选择"));
用Intent分享多张图片
[java] view plaincopy
/**
* 分享多张照片
*/
private void sendMultiple(){
Intent intent=new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, getUriListForImages());
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "你好 ");
intent.putExtra(Intent.EXTRA_TITLE, "我是标题");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "请选择"));
}
/**
* 设置需要分享的照片放入Uri类型的集合里
*/
private ArrayList<Uri> getUriListForImages() {
ArrayList<Uri> myList = new ArrayList<Uri>();
String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/DCIM/100ANDRO/";
File imageDirectory = new File(imageDirectoryPath);
String[] fileList = imageDirectory.list();
if(fileList.length != 0) {
for(int i=0; i<5; i++){
try{
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, fileList[i]);
values.put(Images.Media.DISPLAY_NAME, fileList[i]);
values.put(Images.Media.DATE_TAKEN, new Date().getTime());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode());
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileList[i]);
values.put("_data", imageDirectoryPath + fileList[i]);
ContentResolver contentResolver = getApplicationContext().getContentResolver();
Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
myList.add(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return myList;
}
相关文章推荐
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、人人等
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
- Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码
- android 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)
- 分享到新浪,人人,开心,天涯,豆瓣,搜狐,腾讯等的代码
- Android Intent分享文件|分享图片|分享文字|功能的实现
- 使用Android用自带系统分享功能(填坑,包括微博分享,多图片分享+文字)
- 一键分享文字图片到新浪微博,facebook,twitter 还有保存打印等 (使用 iOS6 自带的 social.framework)
- 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)
- Android:新浪微博拉起客户端分享——完美实现同时分享图片和文字(Intent.ACTION_SEND)
- 一键分享文字图片到新浪微博,facebook,twitter 还有保存打印等 (使用 iOS6 自带的 social.framework)
- 利用Intent一键分享图片文字到微信
- android 不调用微信,易信等SDK实现实现一键分享图片文字到微信,易信,以及全部可选app
- 一键分享文字图片到新浪微博,facebook,twitter 还有保存打印等 (使用 iOS6 自带的 social.framework)
- 一键分享文字图片到新浪微博,facebook,twitter 还有保存打印等 (使用 iOS6 自带的 social.framework)
- Android实现新浪微博SSO授权登录分享文字图片等功能(WEIBO_ANDROID_SDK V2.3.0 )
- Android使用系统Intent实现分享功能及将应用加入分享列表
- Android 如何将图片和文字同时分享到微信