Android call system share pictures and text

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.