将图像存储到SD卡

时间:2013-01-03 09:05:58

标签: android android-imageview android-sdcard android-image

我在我的Android应用程序中使用裁剪功能,它工作正常,但我想将裁剪的图像保存到我的SD Card。为此需要遵循哪些步骤?

3 个答案:

答案 0 :(得分:1)

试试这个:

public void saveBitmap(Bitmap bmp)
{
    String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                                "/NewFolder";
        File dir = new File(file_path);
        if(!dir.exists)
           dir.mkdirs();
        File file = new File(dir, "myImage.png");
        FileOutputStream fOut = new FileOutputStream(file);

        bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
}

Manifest文件中需要以下权限:

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

感谢。

答案 1 :(得分:1)

首先在SD卡中获取图像存储: -

public static String storeImage(Bitmap bitmap, String filename) {

        String stored = null;

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, filename + ".png");

        if (file.exists())
            file.delete();

        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            stored = "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stored;
    }

第二次从SD卡获取图片: -

public static File getImage(String imagename) {

        File mediaImage = null;
        try {
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root);
            if (!myDir.exists())
                return null;

            mediaImage = new File(myDir.getPath() + imagename);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mediaImage;
    }

3将图像从SD卡转换为位图

File file = CommonUtils.getImage("/CoverPic.png");

                String path = file.getAbsolutePath();

                if (path != null)
                    picture = BitmapFactory.decodeFile(path);

答案 2 :(得分:0)

如果您裁剪的Bitmap被称为yourBitmap

File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)