标签:app 文件夹 env filename flush extern Owner card sdc
将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" />
标签:app 文件夹 env filename flush extern Owner card sdc
原文地址:https://www.cnblogs.com/zhaozilongcjiajia/p/10463870.html