Saving and Reading Bitmaps/Images from Internal memory in Android

前端 未结 7 1370
傲寒
傲寒 2020-11-22 02:59

What I want to do, is to save an image to the internal memory of the phone (Not The SD Card).

How can I do it?

I have got the image directly

7条回答
  •  青春惊慌失措
    2020-11-22 03:10

    /**
     * Created by Ilya Gazman on 3/6/2016.
     */
    public class ImageSaver {
    
        private String directoryName = "images";
        private String fileName = "image.png";
        private Context context;
        private boolean external;
    
        public ImageSaver(Context context) {
            this.context = context;
        }
    
        public ImageSaver setFileName(String fileName) {
            this.fileName = fileName;
            return this;
        }
    
        public ImageSaver setExternal(boolean external) {
            this.external = external;
            return this;
        }
    
        public ImageSaver setDirectoryName(String directoryName) {
            this.directoryName = directoryName;
            return this;
        }
    
        public void save(Bitmap bitmapImage) {
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(createFile());
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        @NonNull
        private File createFile() {
            File directory;
            if(external){
                directory = getAlbumStorageDir(directoryName);
            }
            else {
                directory = context.getDir(directoryName, Context.MODE_PRIVATE);
            }
            if(!directory.exists() && !directory.mkdirs()){
                Log.e("ImageSaver","Error creating directory " + directory);
            }
    
            return new File(directory, fileName);
        }
    
        private File getAlbumStorageDir(String albumName) {
            return new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), albumName);
        }
    
        public static boolean isExternalStorageWritable() {
            String state = Environment.getExternalStorageState();
            return Environment.MEDIA_MOUNTED.equals(state);
        }
    
        public static boolean isExternalStorageReadable() {
            String state = Environment.getExternalStorageState();
            return Environment.MEDIA_MOUNTED.equals(state) ||
                    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
        }
    
        public Bitmap load() {
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(createFile());
                return BitmapFactory.decodeStream(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
    

    Usage

    • To save:

      new ImageSaver(context).
              setFileName("myImage.png").
              setDirectoryName("images").
              save(bitmap);
      
    • To load:

      Bitmap bitmap = new ImageSaver(context).
              setFileName("myImage.png").
              setDirectoryName("images").
              load();
      

    Edit:

    Added ImageSaver.setExternal(boolean) to support saving to external storage based on googles example.

提交回复
热议问题