从getExternalStorageDirectory到内部存储

时间:2015-07-02 14:02:04

标签: java android eclipse storage

我再次需要你的帮助。我有这个代码用于简单的照片应用程序,但此代码将已编辑的图像保存在SD卡上,但我想更改此设置以将图像保存在手机的内存中。

private File captureImage() {
    // TODO Auto-generated method stub
    OutputStream output;

    Calendar cal = Calendar.getInstance();

    Bitmap bitmap = Bitmap.createBitmap(ll1.getWidth(), ll1.getHeight(),
            Config.ARGB_8888);

    /*
     * bitmap = ThumbnailUtils.extractThumbnail(bitmap, ll1.getWidth(),
     * ll1.getHeight());
     */
    Canvas b = new Canvas(bitmap);
    ll1.draw(b);

    // Find the SD Card path
    File filepath = Environment.getExternalStorageDirectory();

    // Create a new folder in SD Card
    File dir = new File(filepath.getAbsolutePath() + "/background_eraser/");
    dir.mkdirs();

    mImagename = "image" + cal.getTimeInMillis() + ".png";

    // Create a name for the saved image
    file = new File(dir, mImagename);

    // Show a toast message on successful save
    Toast.makeText(SelectedImgActivity.this, "Image Saved to SD Card",
            Toast.LENGTH_SHORT).show();

    try {

        output = new FileOutputStream(file);
        // Compress into png format image from 0% - 100%
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.flush();
        output.close();
    }

    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return file;

}

有任何建议如何做到这一点?我想我必须将Environment.getExternalStorageDirectory改为其他东西,但是什么?

谢谢!

编辑:

我将此行更改为File filepath = Environment.getDataDirectory(); and I think this works. But this make new folder in root folder...I want it in pictures... How to archive this?

编辑2:

现在我编辑了代码

private File captureImage() {
    // TODO Auto-generated method stub
    OutputStream output;

    Calendar cal = Calendar.getInstance();

    Bitmap bitmap = Bitmap.createBitmap(ll1.getWidth(), ll1.getHeight(),
            Config.ARGB_8888);

    /*
     * bitmap = ThumbnailUtils.extractThumbnail(bitmap, ll1.getWidth(),
     * ll1.getHeight());
     */
    Canvas b = new Canvas(bitmap);
    ll1.draw(b);

    // Find the SD Card path
    File filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//  File filepath = Environment.getDataDirectory(Environment.DIRECTORY_PICTURES);

    // Create a new folder in SD Card
    File dir = new File(filepath.getAbsolutePath() + "/Background Remover/");

    dir.mkdirs();

    mImagename = "image" + cal.getTimeInMillis() + ".png";

    // Create a name for the saved image
    file = new File(dir, mImagename);

    // Show a toast message on successful save
    Toast.makeText(SelectedImgActivity.this, "Image Saved",
            Toast.LENGTH_SHORT).show();

    try {

        output = new FileOutputStream(file);
        // Compress into png format image from 0% - 100%
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.flush();
        output.close();
    }

    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return file;

}

一切正常,除了Toast show ......

1 个答案:

答案 0 :(得分:1)

替换:

File dir = new File(filepath.getAbsolutePath() + "/background_eraser/");

使用:

File dir = context.getFilesDir().getAbsolutePath() + File.separator + "background_eraser";

您可以使用:

FileInputStream fis = context.openFileInput(name);
  

在API级别1中添加

     

返回文件系统上目录的绝对路径,其中存储使用openFileOutput(String,int)创建的文件。