Saving and Reading Bitmaps/Images from Internal memory in Android

前端 未结 7 1356
傲寒
傲寒 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:29

    Came across this question today and this is how I do it. Just call this function with the required parameters

    public void saveImage(Context context, Bitmap bitmap, String name, String extension){
        name = name + "." + extension;
        FileOutputStream fileOutputStream;
        try {
            fileOutputStream = context.openFileOutput(name, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Similarly, for reading the same, use this

    public Bitmap loadImageBitmap(Context context,String name,String extension){
        name = name + "." + extension
        FileInputStream fileInputStream
        Bitmap bitmap = null;
        try{
            fileInputStream = context.openFileInput(name);
            bitmap = BitmapFactory.decodeStream(fileInputStream);
            fileInputStream.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
         return bitmap;
    }
    
    0 讨论(0)
提交回复
热议问题