2019獨角獸企業重金招聘Python工程師標準>>>
public class ImgUtils {public static void saveImageToGallery(Context context, Bitmap bmp) {final String[] items = new String[] { "保存圖片"};//圖片轉成Bitmap數組final Bitmap[] bitmap = new Bitmap[1];bitmap[0] = bmp;// Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
// @Override
// public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
// bitmap[0] = resource;
// }
// });// 首先保存圖片 創建文件夾File appDir = new File(Environment.getExternalStorageDirectory(), "shy");if (!appDir.exists()) {appDir.mkdir();}//圖片文件名稱String fileName = "shy_"+System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();}// 其次把文件插入到系統圖庫String path = file.getAbsolutePath();try {MediaStore.Images.Media.insertImage(context.getContentResolver(), path, fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}// 最后通知圖庫更新Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(file);intent.setData(uri);context.sendBroadcast(intent);new ToastUtils(context).showToast("保存成功");}}
?