Save Image locally in Android

2019-09-14 23:58发布

I am new in Android. I want to store my image(which is chosen from gallery or tack picture from Camera) locally(i.e. SharedPreferences). I want to save my images till application will run in device. Once application will be removed from device then all data will be removed.

Can anyone help me?

Thanks.

标签: android
1条回答
一夜七次
2楼-- · 2019-09-15 00:26

call this function for save..

void saveImage() {

File myDir=new File("/sdcard/saved_images");
    myDir.mkdirs();

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
           final AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
                alertDialog.setTitle("Save");  
                alertDialog.setMessage("Your drawing had been saved:)");  
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) { 
                    return;  
                }  
            });  
            alertDialog.show();
    } catch (Exception e) {
           e.printStackTrace();
    }
}
查看更多
登录 后发表回答