先将图片url或本地图片转成bitmap 然后
本地图片转Bitmap
Bitmap bitmap = BitmapFactory.
decodeResource(getResources()
, R.drawable.
ceshi)
;
调用该方法
public static void
saveImageToGallery(Context context
, Bitmap bmp) {
// 首先保存图片 OOOO是保存图片的文件夹名字
File appDir = new File(Environment. getExternalStorageDirectory() , "OOOO") ;
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://" + Environment. getExternalStorageDirectory()))) ;
}
// 首先保存图片 OOOO是保存图片的文件夹名字
File appDir = new File(Environment. getExternalStorageDirectory() , "OOOO") ;
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://" + Environment. getExternalStorageDirectory()))) ;
}