//分享文字 public static void shareText(Context context) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "test"); shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my Share text."); shareIntent.setType("text/plain"); //设置分享列表的标题,并且每次都显示分享列表 context.startActivity(Intent.createChooser(shareIntent, "分享到")); } //分享单张图片 public static void shareSingleImage(Context context) { String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg"; //由文件得到uri Uri imageUri = Uri.fromFile(new File(imagePath)); Log.d("share", "uri:" + imageUri); //输出:file:///storage/emulated/0/test.jpg Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); shareIntent.setType("image/*"); context.startActivity(Intent.createChooser(shareIntent, "分享到")); } //分享多张图片 public static void shareMultipleImage(Context context) { ArrayList uriList = new ArrayList<>(); String path = Environment.getExternalStorageDirectory() + File.separator; uriList.add(Uri.fromFile(new File(path+"australia_1.jpg"))); uriList.add(Uri.fromFile(new File(path+"australia_2.jpg"))); uriList.add(Uri.fromFile(new File(path+"australia_3.jpg"))); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.setType("image/*"); context.startActivity(Intent.createChooser(shareIntent, "分享到")); } public static void shareMsg(Context context,String activityTitle, String msgTitle, String msgText, String imgPath) { Intent intent = new Intent(Intent.ACTION_SEND); if (imgPath == null || imgPath.equals("")) { intent.setType("text/plain"); // 纯文本 } else { File f = new File(imgPath); if (f != null && f.exists() && f.isFile()) { intent.setType("image/jpg"); Uri u = Uri.fromFile(f); intent.putExtra(Intent.EXTRA_STREAM, u); } } intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle); intent.putExtra(Intent.EXTRA_TEXT, msgText); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(Intent.createChooser(intent, activityTitle)); } public static void shareMsg2(Context context,String activityTitle, String msgTitle, String msgText, File file) { Intent intent = new Intent(Intent.ACTION_SEND); if (file == null ) { intent.setType("text/plain"); // 纯文本 } else { if (file != null && file.exists() && file.isFile()) { intent.setType("image/jpg"); Uri uri=null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Log.e("url", FileUtils.getApplicationId(context) + ".fileprovider" ); uri = FileProvider.getUriForFile(context, "com.geekbuy.geekbuying.fileprovider", file); } else { uri = Uri.fromFile(file); } if(uri!=null) { intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra(Intent.EXTRA_STREAM, uri); } } } intent.putExtra(Intent.EXTRA_TEXT, "https://m.geekbuying.com/item/Global-Version-Xiaomi-Redmi-Note-5-5-99-Inch-4GB-64GB-Smartphone-Black-396981.html"); intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(Intent.createChooser(intent, activityTitle));
}
DownLoadImage downLoadImage= new DownLoadImage(url, new ImageDownView() { @Override public void onDownLoadSuccess(File imageFile) { Log.e("url", imageFile.getPath()+" ;---"+imageFile.getAbsolutePath()); SysShareUtil.shareMsg2(GoodsDetailsActivity.this,"test1","test2","ee",imageFile); } @Override public void onDownLoadFailed() { } }); downLoadImage.downloadImage();
public class DownLoadImage { private String url; private Context context; private ImageDownView callBack; private Bitmap bitmap; private File currentFile; public DownLoadImage(String url, ImageDownView callBack) { context = GKApplication.getInstance().getApplicationContext(); this.url = url; this.callBack = callBack; } public void downloadImage() { new Thread(new Runnable() { @Override public void run() { try { String url = "http://www.guolin.tech/book.png"; FutureTarget<Bitmap> target = Glide.with(context) .asBitmap() .load(url) .submit(); if (target != null) { // 在这里执行图片保存方法 bitmap = target.get(); if (bitmap != null) { // 在这里执行图片保存方法 saveImageToGallery(bitmap); } } } catch (Exception e) { e.printStackTrace(); } finally { if (bitmap != null && currentFile.exists()) { callBack.onDownLoadSuccess(currentFile); } else { callBack.onDownLoadFailed(); } } } }).start(); } // public void run() { // try { // FutureTarget<Bitmap> target = Glide.with(context) // .asBitmap() // .load(url) // .submit(); // if (target != null){ // // 在这里执行图片保存方法 // bitmap = target.get(); // if(bitmap!=null){ // // 在这里执行图片保存方法 // saveImageToGallery(bitmap); // } // } // } catch (Exception e) { // e.printStackTrace(); // } finally { // if (bitmap != null && currentFile.exists()) { // callBack.onDownLoadSuccess(currentFile); // } else { // callBack.onDownLoadFailed(); // } // } // } public void saveImageToGallery(Bitmap bmp) { // 首先保存图片 File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径 String fileName = "geek"; File appDir = new File(file, fileName); if (!appDir.exists()) { appDir.mkdirs(); } fileName = System.currentTimeMillis() + ".jpg"; currentFile = new File(appDir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(currentFile); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }