在Android中本地保存图像

时间:2011-09-05 08:40:27

标签: android

我是Android新手。我想在本地存储我的图像(从相机中选择图库或粘贴图片)(即SharedPreferences)。我想保存我的图像,直到应用程序在设备中运行。一旦应用程序从设备中删除,那么所有数据都将被删除。

任何人都可以帮助我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

调用此函数进行保存..

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();
    }
}