Image is not saved in the folder

I am trying to create a folder and save images in it.
But that does not work.
I do not know what is wrong in my code - could you explain to me why?

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


    }

change

        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();
        }
+4
source share
1 answer

How do I make a folder in DCIM and create a file there:

    /*
    Create a path where we will place our picture in the user 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();
    }

fileJPGis the name of the file that I am creating (dynamically by adding a date).
Replace MyFolderNamewith albumName.
bmp- this is my Bitmap data (screenshot, in my case).

+5
source

Source: https://habr.com/ru/post/1614932/


All Articles