网络图片url或本地图片 转bitmap保存到系统相册
2016-11-04 14:56
387 查看
先将图片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())));
}
本地图片转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())));
}
相关文章推荐
- Android下载网络图片并保存在本地相册
- Java使用URL读取网络图片资源 并保存在本地
- android 读取本地或网络图片,转换为Bitmap 并保存本地中
- 使用okhttp异步下载图片,保存到本地,并在系统相册中显示
- 通过网络Url下载图片并下载到本地相册
- Android ImageUtils 图片工具类,包括:小米从相册调图像空指针,三星旋转照片,压缩图片,保存Bitmap到本地,获取本地视频的缩略图,毛化玻璃效果,代码以后会更新
- Android点击图片显示大图,长按保存网络图片到本地相册,单击返回。(GestureDetector)(RxJava)(Easypermissions)(AlertView)
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- 新手使用长按手势将网络图片保存至系统相册出现的问题
- 【学习ios之路:UI系列】获取通过UIImagePackerController获取的系统相册图片的名称信息及保存系统相册到本地
- 保存图片到本地,并及时在系统相册更新
- 将图片保存在本地相册中
- 保存网络图片到本地
- 从网络上进行下载图片,并保存到本地
- java 下载网络上的图片并保存到本地目录
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- 将Bitmap保存为本地图片
- ios uiwebview 长按图片,保存到本地相册
- ios uiwebview 长按图片,保存到本地相册
- Android 保存图片到系统相册