将图片保存到系统相册的两种方法
来源:互联网 发布:淘宝 cpu 主板 编辑:程序博客网 时间:2024/04/30 01:22
第一种:采用系统的api直接使用:
ContentResolver cr = getContentResolver();String url = MediaStore.Images.Media.insertImage(cr, bmp,String.valueOf(System.currentTimeMillis()), "");
但是,这种方式必须得刷新图库:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
尽管如此,这种方法还是只能适合安卓4.4以下的手机,若是4.4以上的手机就会报错,因此建议采用第二种方式来写;
第二种:直接采用文件流进行保存到相册
File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + String.valueOf(System.currentTimeMillis()) + ".png");if(tempFile.exists()){tempFile.delete();}try {tempFile.createNewFile();} catch (IOException e) {e.printStackTrace();}FileOutputStream fOut = null;try {fOut = new FileOutputStream(tempFile);} catch (FileNotFoundException e) {e.printStackTrace();}bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);try {fOut.flush();fOut.close();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}
最后把整个方法贴出来:
/** * 將ImageView中的圖片保存到系统相册 */private void SaveImageToSysAlbum() {if (FileUtil.isSdCardExist()) {BitmapDrawable bmpDrawable = (BitmapDrawable)mFullImageView.getDrawable();Bitmap bmp = bmpDrawable.getBitmap();if (bmp != null) {try {/*ContentResolver cr = getContentResolver();String url = MediaStore.Images.Media.insertImage(cr, bmp,String.valueOf(System.currentTimeMillis()), "");*/File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + String.valueOf(System.currentTimeMillis()) + ".png");if(tempFile.exists()){tempFile.delete();}try {tempFile.createNewFile();} catch (IOException e) {e.printStackTrace();}FileOutputStream fOut = null;try {fOut = new FileOutputStream(tempFile);} catch (FileNotFoundException e) {e.printStackTrace();}bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);try {fOut.flush();fOut.close();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}Toast.makeText(this, getString(R.string.save_succ), Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();}}else {Toast.makeText(this, getString(R.string.no_iamge_save_fail), Toast.LENGTH_SHORT).show();}}else {Toast.makeText(this, getString(R.string.no_sdcard_save_fail), Toast.LENGTH_SHORT).show();}String release = android.os.Build.VERSION.RELEASE;String tempID = release.substring(0, 3);if(Double.parseDouble(tempID) >= 4.4){//安卓4.4以上版本的时候使用这个,以下的使用else语句里面的MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null);}else {sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));MediaScannerConnection.scanFile(this,new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" }, null,null); }}
0 0
- 将图片保存到系统相册的两种方法
- 将图片保存到系统相册的两种方法
- 将图片保存到系统相册
- 将图片保存到相册
- 将图片保存到相册
- 将图片保存到相册
- 解决保存图片到系统相册出现两张图片的问题
- 保存图片到系统相册
- 保存图片到系统相册
- 保存图片到系统相册
- 将UIImageView上面的图片内容保存到相册
- 将相机/相册的图片保存到本地沙盒
- 截屏,并将截取的图片保存到相册中
- UWP将图片保存到相册的文件下
- 将UIImageView上的图片保存到相册中
- 将图片保存到相册的工具类
- 将图片保存到ios相册
- 将图片保存到相册中
- 弹出框
- unity发布android包
- GXT之旅:项目源码下载
- TimeRecordRespective
- WebServices 简介
- 将图片保存到系统相册的两种方法
- Activity的跳转
- jQuery代码优化:事件委托篇
- Linux Kbuild文档 (介绍的比较规范的一个)
- 用ajax提交到后台方法,返回一个json数组,在前台怎么去将这个json数据读取出来放在table里面。
- 测试REST接口的Chrome插件postman
- 如何设置Apache虚拟主机在Ubuntu12.04 LTS
- DEDECMS增加自定义属性完整教程
- 兰旭--如何抢占手游社交红利?