4

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();
        }
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • 2
    As expert Stackoverflow user, you should be more specific than "It is not working" – Meier Nov 08 '15 at 11:32
  • @FrnankN.Stein well I am still learning about them , thanks for clarifying , i though the filename3 is the name of the image not folder. – Moudiz Nov 08 '15 at 11:37
  • @FrankN.Stein I edit my question, I add how I modified my code but still the folder not created , can you help me with that ? – Moudiz Nov 08 '15 at 12:28
  • @FrankN.Stein then why the folder and the image is not created ? please note that I have add the permisions – Moudiz Nov 08 '15 at 12:34
  • Check out my answer. It's live code, and it works. – Phantômaxx Nov 08 '15 at 12:36

2 Answers2

6

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).

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • still the folder didnt created. where I am doing wrong? the onactivityresult is working and the image that I am selecting is being upload to server. – Moudiz Nov 08 '15 at 12:39
  • Weird. I use this code and it's working. You already set the `` permission, right? By the way, it's the only one needed, since it implies the READ one too. – Phantômaxx Nov 08 '15 at 12:41
  • ` ` yeap , ill edit how I write the code maybe you can indetify a mistake – Moudiz Nov 08 '15 at 12:42
  • Now you forgot to add the code which writes the file to the storage. – Phantômaxx Nov 08 '15 at 12:46
  • Yes, that one. Anyway, at least the folder should have been created. – Phantômaxx Nov 08 '15 at 12:51
  • the folder didnt created because its empty, I add the outputstream but the image wasent created , I edit my question, is it correct how I have write the outputstream ? by the way thanks or helping me. ah ok now i saw the edit ill check it – Moudiz Nov 08 '15 at 12:56
  • I guess you mean: The folder HAS been created. AND it is empty. That is OK. So the folder is in place, only the file needs to be created. Now, have a look at my updated code - there's a slight difference with yours. – Phantômaxx Nov 08 '15 at 12:59
  • I got an error `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference` on this line `final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);`, is it necessary ? – Moudiz Nov 08 '15 at 13:04
  • sorry the folder wasent created , I read something on SO saying that if a folder is empty it cannot be created. I tryd your code but its giving me error. – Moudiz Nov 08 '15 at 13:10
  • No, it's not. You can use `fos = new FileOutputStream(mypath); bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);` – Phantômaxx Nov 08 '15 at 13:12
  • Every folder is empty when it is created. THEN it's filled with something. You read something silly. If you don't want to use `Environment.DCIM`, you might want to use `Environment.DIRECTORY_PICTURES`. – Phantômaxx Nov 08 '15 at 13:12
  • the folder was created now , but the image is broken cannot be open , can you please add in your asnwer the bm how you are creating it ? it seems i am creating it wrong – Moudiz Nov 08 '15 at 14:13
  • It's just a screenshot (I need to give the user the ability to share a chart). It's not open from the gallery. But I'd really change this `Bitmap imageToSave = (Bitmap) data.getExtras().get("data");` to `Bitmap imageToSave = (Bitmap) data.getData();` – Phantômaxx Nov 08 '15 at 14:14
  • I dont know why the image is not created in the folder. do you recomand to open a new SO question ? .please note i added an edit how the code is now. – Moudiz Nov 08 '15 at 14:23
  • when I added it I got this error `iconvertibale types:cannot cast 'android.net.uri ' to android graphics bitmap` – Moudiz Nov 08 '15 at 14:27
1

i take a long time for this faking error too and finally it's solve just with add this one line code in manifest

    android:requestLegacyExternalStorage="true"