0

I am stuck with this from this morning: the image is not being created in the folder.
Can you help me and tell me why it's not working?

Is this correct for getting the image?

Bundle extras = data.getExtras();
        Bitmap imageToSave = extras.getParcelable("data");

This is my full code

@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) {



            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");
           // Bitmap imageToSave = (Bitmap) data.getData();
            Bundle extras = data.getExtras();
            Bitmap imageToSave = extras.getParcelable("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();
            }
            Uri selectedImage = data.getData();

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


        }
    }}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Moudiz
  • 7,211
  • 22
  • 78
  • 156

2 Answers2

2

This should help:

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cur =
            getContentResolver().query
            (
                selectedImage, filePathColumn, null, null, null
            );
        cur.moveToFirst();

        String picturePath = cur.getString(cur.getColumnIndex(filePathColumn[0]));
        cur.close();

        // String picturePath contains the path of the selected Image
        // Now you can copy it, send it to a server, load it into an ImageView...
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I copied your code as it is , and the image wasent save , what am I missing, how you are specifying the folder name ? – Moudiz Nov 08 '15 at 15:02
  • 1
    I made more search about your code and I got this example and it helped me too http://www.geeks.gallery/saving-image/ .. thank for your helping me – Moudiz Nov 08 '15 at 15:19
  • before I post a new question , do you know a link or site for this ? I have a grid view, what I want when I click on image on this grid to be shown on an activity, useally I used to this with an intent, but for better performance its better to get it from the folder. so how to read the selected image in the gridview from the folder ? – Moudiz Nov 08 '15 at 15:22
  • How to pass the string ? I mean lets say i clicked on image X how would send the path of image x? – Moudiz Nov 08 '15 at 15:42
1

Data.getExtra() retrun a Uri;you should transform it to filepath.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42