Android 图像未保存在文件夹中

Android 图像未保存在文件夹中,android,Android,我正在尝试创建文件夹并在其中保存图像。 但它不起作用。 我不知道我的代码出了什么问题-你能告诉我为什么吗 // The method that invoke of uploading images public void openGallery() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT);

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

    // 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);


    }
编辑


如何在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:requestLegacyExternalStorage="true"

作为Stackoverflow专家用户,您应该比“它不起作用”更具体@FrnankN.Stein我仍在了解它们,感谢您的澄清,我认为filename3是图像的名称,而不是文件夹。@FrankN.Stein我编辑了我的问题,我添加了我如何修改代码,但仍然没有创建文件夹,你能帮我吗?@FrankN.Stein那么为什么不创建文件夹和图像?请注意,我已经添加了许可证请勾选我的答案。这是一个实时代码,可以正常工作。但文件夹仍然没有创建。我哪里做错了?onactivityresult正在运行,我选择的图像正在上载到server.com。我使用这个代码,它是有效的。您已经设置了
权限,对吗?顺便说一句,它是唯一需要的,因为它也意味着读代码。“是的,我会编辑我写代码的方式,也许你可以确定错误现在你忘了添加将文件写入存储器的代码。是的,那一个。无论如何,至少应该已经创建了文件夹。
    android:requestLegacyExternalStorage="true"