码迷,mamicode.com
首页 > 其他好文 > 详细

图片Bitmap在本地的存储与读取 File

时间:2019-03-03 09:58:47      阅读:396      评论:0      收藏:0      [点我收藏+]

标签: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" />

 

图片Bitmap在本地的存储与读取 File

标签:app   文件夹   env   filename   flush   extern   Owner   card   sdc   

原文地址:https://www.cnblogs.com/zhaozilongcjiajia/p/10463870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!