且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android的本地保存图片

更新时间:2023-02-02 20:52:39

调用这个函数保存。

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