gpt4 book ai didi

android - 图像未保存在文件夹中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:25 24 4
gpt4 key购买 nike

我正在尝试创建一个文件夹并在其中保存图像。
但它不起作用。
我不知道我的代码有什么问题 - 你能告诉我为什么吗?

    // The method that invoke of uploading images
public void openGallery() {


Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

File folder = new File(this.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), "albumName");


File file = new File(this.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), "fileName"+3);
Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Uri selectedImage = data.getData();


Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagePath", selectedImage.toString());
startActivity(i);


}

编辑

        final File path =
Environment.getExternalStoragePublicDirectory
(
// Environment.DIRECTORY_PICTURES + "/ss/"
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/MyFolderName/"
);

// Make sure the Pictures directory exists.
if(!path.exists())
{
path.mkdirs();
}
Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
final File file = new File(path, "file" + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(path);
final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

FileOutputStream out = new FileOutputStream(path);
//fos = new FileOutputStream(path);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

我如何在 DCIM 中创建文件夹并在其中创建文件:

    /*
Create a path where we will place our picture in the user's public
pictures directory. Note that you should be careful about what you
place here, since the user often manages these files.
For pictures and other media owned by the application, consider
Context.getExternalMediaDir().
*/
final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/MyFolderName/"
);

// Make sure the Pictures directory exists.
if(!path.exists())
{
path.mkdirs();
}

final File file = new File(path, fileJPG + ".jpg");

try
{
final FileOutputStream fos = new FileOutputStream(file);
final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

//bmp.compress(CompressFormat.JPEG, 100, bos);
bmp.compress(CompressFormat.JPEG, 85, bos);

bos.flush();
bos.close();
}
catch (final IOException e)
{
e.printStackTrace();
}

fileJPG 是我正在创建的文件名(动态添加日期)。
MyFolderName 替换为 albumName
bmp 是我的位图数据(在​​我的例子中是屏幕截图)。

关于android - 图像未保存在文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593342/

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