Call system Share text:
public static void Sharetext (context context, String Extratext) {
Intent Intent = new Intent (intent.action_send);
Intent.settype ("Text/plain");
Intent.putextra (Intent.extra_subject, "Connection Sharing");
Intent.putextra (Intent.extra_text, Extratext);
Intent.setflags (Intent.flag_activity_new_task);
Context.startactivity (
Intent.createchooser (Intent, "Connection Sharing"));
}
调用系统分享图片,方法是: 1、先读取Assets里面的图片转化成Bitmap ; 2、再以文件File形式保存在本地; 3、最后Uri连接本地该图片进行分享。 读取Assets里面的图片转化成Bitmap,代码如下: private Bitmap getImageFromAssetsFile(String fileName){ Bitmap image = null; AssetManager am = getResources().getAssets(); try { InputStream is = am.open(fileName); image = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return image;}
Bitmap is stored locally as a file, and the code is as follows:
public static File saveFile (Bitmap bm,string path, String fileName) throws IOException {
File Dirfile = new file (path);
if (!dirfile.exists ()) {
Dirfile.mkdir ();
}
File Mycapturefile = new file (path, fileName);
Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (Mycapturefile));
Bm.compress (Bitmap.CompressFormat.JPEG, N, BOS);
Bos.flush ();
Bos.close ();
return mycapturefile;
}
调用系统原生分享图片代码: public static void shareImage(Context context, Uri uri, String title) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.setType("image/jpeg"); context.startActivity(Intent.createChooser(shareIntent, title));} 最后Uri连接本地该图片进行分享: Bitmap bitmap = getImageFromAssetsFile("/assets/ewcode.png"); try { File file = saveFile(bitmap, dir, "ewcode.png"); Uri uri = Uri.fromFile(file); Shares.shareImage(EWcodeActivity.this,uri,"二维码分享"); } catch (IOException e) { e.printStackTrace(); }
Android call system share pictures and text