gpt4 book ai didi

android:将文件保存到特定文件夹以供以后检索

转载 作者:太空狗 更新时间:2023-10-29 16:43:15 24 4
gpt4 key购买 nike

我正在画图部分,写了下面的代码把图片保存到指定的相机文件夹中。但是,我宁愿使用应用程序名称创建一个新文件夹并将图像保存到该文件夹​​中。这是怎么做到的?

我还想稍后从该特定文件夹中检索图像文件。

谢谢!

当前代码:

     String fileName = "ABC";

// create a ContentValues and configure new image's data
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, fileName);
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpg");

// get a Uri for the location to save the file
Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

try
{
OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

outStream.flush(); // empty the buffer
outStream.close(); // close the stream

最佳答案

试试这个,它可能对你有帮助。

public void saveImageToExternalStorage(Bitmap image) {
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/directoryName";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(fullPath, "image.png");
if(file.exists())
file.delete();
file.createNewFile();
fOut = new FileOutputStream(file);
// 100 means no compression, the lower you go, the stronger the compression
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
Log.e("saveToExternalStorage()", e.getMessage());
}
}

关于android:将文件保存到特定文件夹以供以后检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14364391/

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