Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image not saving in folder

Tags:

android

I am trying to create a folder and save images in it.
But it's not working.
I don't know what's wrong in my code - can you tell 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);


    }

edit

        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();
        }
like image 479
Moudiz Avatar asked Mar 15 '23 05:03

Moudiz


1 Answers

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

    /*
    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 is the file name I'm creating (dynamically, adding a date).
Replace MyFolderName with albumName.
bmp is my Bitmap data (a screenshot, in my case).

like image 55
Phantômaxx Avatar answered Mar 30 '23 12:03

Phantômaxx