download.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { { if(imgsUrl!=null){ final int currentItem = image_pager.getCurrentItem(); new Thread(new Runnable() { @Override public void run() { try { final Bitmap myBitmap = Glide.with(BaseApplication.getmApplication())//上下文 .load(imgsUrl.get(currentItem))//url .asBitmap() //必須 .centerCrop() .into(500, 500) .get() ; Log.d("sevaImage", "onClick: "+myBitmap.getByteCount()); runOnUiThread(new Runnable() { @Override public void run() { SaveImageUtils.saveImageToGallerys(ImageShowActivity.this,myBitmap); } }); } catch (Exception e) { e.printStackTrace(); } } }).start(); } } } });
package com.uctimes.jike.util; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import com.uctimes.jike.BaseApplication; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Created by uctimes on 2016/10/25. */ public class SaveImageUtils { public static void saveImageToGallery(Context context, Bitmap bmp) { // 首先保存圖片 File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = 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 (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統圖庫 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath()))); } public static void saveImageToGallerys(Context context, Bitmap bmp) { if (bmp == null){ ToastUtils.show(context, "保存出錯了..."); return; } // 首先保存圖片 // File appDir = new File(BaseApplication.app.getTmpDir(), "ywq"); File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = 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 (FileNotFoundException e) { ToastUtils.show(context, "文件未發現"); e.printStackTrace(); } catch (IOException e) { ToastUtils.show(context, "保存出錯了..."); e.printStackTrace(); }catch (Exception e){ ToastUtils.show(context, "保存出錯了..."); e.printStackTrace(); } // 最后通知圖庫更新 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), 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); ToastUtils.show(context, "保存成功了..."); } }
