将图像保存在SD卡中

时间:2012-10-26 14:31:17

标签: android sd-card

我已经编写了用于编辑图像的代码,现在我想将该编辑的图像保存在SD卡中

image=(ImageView)findViewById(R.id.image);
        Intent intent = getIntent();
        File sdCardDirectory = Environment.getExternalStorageDirectory();
        photo = (Bitmap) intent.getParcelableExtra("photoo");
        image.setImageBitmap(photo);

3 个答案:

答案 0 :(得分:5)

试试这个,

 void saveImage() {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

答案 1 :(得分:1)

首先,你需要在AndroidManifest.xml中给出写权限,如下所示,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

现在让我们看一下实际编写已编辑图像的代码,

// Getting the SDCard Path
File sdcard = Environment.getExternalStorageDirectory();
File editedFile = new File( sdcard, "myphoto.jpeg" );

// if file is already exists then first delete it
if ( editedFile.exists() ) 
{   
    editedFile.delete(); 
}

FileOutputStream fOut = new FileOutputStream( editedFile );
photo.compress( Bitmap.CompressFormat.JPEG, 90, fOut );

答案 2 :(得分:0)

这很简单。

File outpt = new File( sdCardDirectory, "photo.jpeg" );
FileOutputStream outptOs = new FileOutputStream( outpt );
photo.compress( Bitmap.CompressFormat.JPEG, 90, outptOs );