1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class SaveToAlbumUtil {
public static void saveBitmapToGallery(Bitmap bitmap, String picName, Context context) {
String fileName = null; String galleryPath= Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM +File.separator+"Camera"+File.separator;
File file = null; FileOutputStream outStream = null;
try { file = new File(galleryPath, picName+ ".jpeg");
fileName = file.toString(); outStream = new FileOutputStream(fileName); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
} catch (Exception e) { e.getStackTrace(); }finally { try { if (outStream != null) { outStream.close(); } } catch (IOException e) { e.printStackTrace(); } }
MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, fileName, null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(file); intent.setData(uri); context.sendBroadcast(intent);
Toast.makeText(context, "图片保存成功", Toast.LENGTH_SHORT).show(); } }
|