安卓呼叫系統自帶分享功能分享文字,分享大圖片,仿好奇心日報分享長圖片(不用申請微信微博官方sdk就能直接分享)
阿新 • • 發佈:2019-01-31
當前安卓分享一共三種方式
1,呼叫安卓系統自帶分享功能(可以分享大圖)
2,呼叫微信,微博支付寶等自己的官方分享sdk
3,用友盟,shareSdk等整合好的sdk
由於公司業務要求,需要分享一些長圖,大圖到微信,微博等。由於微信微博自己的官方sdk對圖片有限制,
比如微博要求縮圖不能大於32k,原圖不能大於2M.所以今天來給大家講下不通過任何sdk,直接呼叫安卓系統自帶的分享功能
安卓系統自帶的分享功能可以分享文字,長圖片,下面就分別給大家講解下
一,系統自帶分享文字
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "標題");
intent.putExtra(Intent.EXTRA_TEXT, "描述資訊" + "這裡你可以追加一個url連線");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "分享到"));
二,系統自帶分享大圖片之最簡單的是直接分享本地圖片
String path = Environment.getExternalStorageDirectory() + File.separator+;//sd根目錄
File file = new File(path, "share" + ".jpg");//這裡share.jpg是sd卡根目錄下的一個圖片檔案
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
三,通常我們分享的圖片不可能是本地存好的圖片,我們通常分享的是自己生成的圖片獲者網路圖片
這裡簡單給大家介紹一個仿好奇心日報那樣的把本地佈局截圖分享出去
1,首先獲取截圖
/*
* 將佈局轉化為bitmap
這裡傳入的是你要截的佈局的根View
* */
public Bitmap getBitmapByView(View headerView) {
int h = headerView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
headerView.draw(canvas);
return bitmap;
}
2,把截圖獲取的bitmap做簡單的壓縮
/*
* 壓縮圖片
* */
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 400) { //迴圈判斷如果壓縮後圖片是否大於400kb,大於繼續壓縮(這裡可以設定大些)
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裡壓縮options%,把壓縮後的資料存放到baos中
options -= 10;//每次都減少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的資料baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream資料生成圖片
return bitmap;
}
3,把壓縮過的圖片先儲存到本地才能呼叫系統分享出去,因為系統分享的是一個uri,我們需要先把bitmap轉為本地file檔案
再把file轉換為uri
/*
* 把bitmap轉化為file
* */
public File bitMap2File(Bitmap bitmap) {
String path = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory() + File.separator;//儲存到sd根目錄下
}
// File f = new File(path, System.currentTimeMillis() + ".jpg");
File f = new File(path, "share" + ".jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return f;
}
}
4,呼叫上面的方法獲取到file,轉換為uri並分享出去
File file = bitMap2File(compressImage);
if (file != null && file.exists() && file.isFile()) {
//由檔案得到uri
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
}
四,運用ComponentName來指定分享到哪裡
1,指定是分享到微信好友
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片給好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
2,指定分享到朋友圈
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片到朋友圈
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
3,指定分享到qq
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片到qq
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
1,呼叫安卓系統自帶分享功能(可以分享大圖)
2,呼叫微信,微博支付寶等自己的官方分享sdk
3,用友盟,shareSdk等整合好的sdk
由於公司業務要求,需要分享一些長圖,大圖到微信,微博等。由於微信微博自己的官方sdk對圖片有限制,
比如微博要求縮圖不能大於32k,原圖不能大於2M.所以今天來給大家講下不通過任何sdk,直接呼叫安卓系統自帶的分享功能
安卓系統自帶的分享功能可以分享文字,長圖片,下面就分別給大家講解下
一,系統自帶分享文字
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "標題");
intent.putExtra(Intent.EXTRA_TEXT, "描述資訊" + "這裡你可以追加一個url連線");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "分享到"));
二,系統自帶分享大圖片之最簡單的是直接分享本地圖片
String path = Environment.getExternalStorageDirectory() + File.separator+;//sd根目錄
File file = new File(path, "share" + ".jpg");//這裡share.jpg是sd卡根目錄下的一個圖片檔案
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
三,通常我們分享的圖片不可能是本地存好的圖片,我們通常分享的是自己生成的圖片獲者網路圖片
這裡簡單給大家介紹一個仿好奇心日報那樣的把本地佈局截圖分享出去
1,首先獲取截圖
/*
* 將佈局轉化為bitmap
這裡傳入的是你要截的佈局的根View
* */
public Bitmap getBitmapByView(View headerView) {
int h = headerView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
headerView.draw(canvas);
return bitmap;
}
2,把截圖獲取的bitmap做簡單的壓縮
/*
* 壓縮圖片
* */
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 400) { //迴圈判斷如果壓縮後圖片是否大於400kb,大於繼續壓縮(這裡可以設定大些)
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裡壓縮options%,把壓縮後的資料存放到baos中
options -= 10;//每次都減少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的資料baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream資料生成圖片
return bitmap;
}
3,把壓縮過的圖片先儲存到本地才能呼叫系統分享出去,因為系統分享的是一個uri,我們需要先把bitmap轉為本地file檔案
再把file轉換為uri
/*
* 把bitmap轉化為file
* */
public File bitMap2File(Bitmap bitmap) {
String path = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory() + File.separator;//儲存到sd根目錄下
}
// File f = new File(path, System.currentTimeMillis() + ".jpg");
File f = new File(path, "share" + ".jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return f;
}
}
4,呼叫上面的方法獲取到file,轉換為uri並分享出去
File file = bitMap2File(compressImage);
if (file != null && file.exists() && file.isFile()) {
//由檔案得到uri
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
}
四,運用ComponentName來指定分享到哪裡
1,指定是分享到微信好友
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片給好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
2,指定分享到朋友圈
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片到朋友圈
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
3,指定分享到qq
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由檔案得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//傳送圖片到qq
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享圖片"));
有問題可以加我微信2501902696