图片Bitmap在本地的存储与读取 File
Posted zhaozilongcjiajia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片Bitmap在本地的存储与读取 File相关的知识,希望对你有一定的参考价值。
将Bitmap存储到本地:
private void SaveImage(Bitmap image, String user_id){ //照片通常存在DCIM文件夹中 String sdCardDir = Environment.getExternalStorageDirectory()+"/DCIM/"; //为APP创建一个文件夹来存储图片 File appDir = new File(sdCardDir, "MyGraduation"); if(!appDir.exists()){ appDir.mkdir(); } //图片名称 String fileName = user_id+".jpg"; //设置图片路径 File imageDir = new File(appDir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(imageDir); image.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
从本地读取到Bitmap文件:
private Bitmap getImage(String user_id){ String path = Environment.getExternalStorageDirectory()+"/DCIM/MyGraduation/"+user_id+".jpg"; Bitmap bitmap = null; FileInputStream fis = null; try { fis = new FileInputStream(path); bitmap = BitmapFactory.decodeStream(fis); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; }
读取和改写SD卡需要权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_OWNER_DATA" />
以上是关于图片Bitmap在本地的存储与读取 File的主要内容,如果未能解决你的问题,请参考以下文章