Android calls the system to share pictures and text

Call the system to 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, "Connect to share ");
intent.putExtra(Intent.EXTRA_TEXT, extraText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(
Intent.createChooser(intent, "connect to share"));
}

To call the system to share pictures, the methods are:
1. Put the picture in the file assets
2. First read the picture in the assets and convert it into Bitmap;
3. Then save it locally in the form of a file;
4. Finally, Uri connects the picture locally to perform share.

Call the system native sharing image code:

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));
}

Finally, Uri connects to the local image to share:

方法一:
// 读取Assets里面的图片转化成Bitmap
private static Bitmap getImageFromAssetsFile(Context context,String fileName){
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try {
InputStream is=am.open(fileName);
image= BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace(); }
return image;
}

// Bitmap以文件File形式保存在本地
private static Uri saveBitmap(Bitmap bm, String picName) {
try {
String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+"/zqhd/"+picName+".jpg";
File f = new File(dir);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
Uri uri = Uri.fromFile(f);
return uri;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
return null;
}

public static void localshare(Context context,String pic) {
/* Share image /
Bitmap bgimg0 = getImageFromAssetsFile(context,pic+".png");
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);// Set sharing behavior
share_intent.setType("image/
"); //Set the type of shared content
share_intent.putExtra(Intent.EXTRA_STREAM, saveBitmap(bgimg0,pic));
//Create a shared Dialog
share_intent = Intent.createChooser(share_intent, "Share image");
context.startActivity(share_intent);
}

Method Two:

public static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zuhd/";

// Bitmap以文件File形式保存在本地
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, 80, bos);
bos.flush();
bos.close();
return myCaptureFile;
}

public static void sharePic(){
 Bitmap  bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ewcode);
               File file=null;
            try {
                file = saveFile(bitmap, dir, "ewcode.jpg");

            } catch (IOException e) {
                e.printStackTrace();
            }
                                 Uri uri = Uri.fromFile(file);
                                 Shares.shareImage(EWcodeActivity.this,uri,"二维码分享");

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325367939&siteId=291194637