gpt4 book ai didi

java - 从 getExternalStorageDirectory 到内部存储

转载 作者:行者123 更新时间:2023-11-30 02:03:01 25 4
gpt4 key购买 nike

我再次需要你的帮助。我有简单照片应用程序的代码,但此代码将编辑后的图像保存在 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();我认为这行得通。但这会在根文件夹中创建新文件夹...我想要它在图片中...如何存档?

编辑 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 显示...

最佳答案

替换:

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

与:

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

您可以使用:

FileInputStream fis = context.openFileInput(name);

Added in API level 1

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

关于java - 从 getExternalStorageDirectory 到内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com