jeudi 16 juin 2016

Pick an image and save it in custom folder

I need your help to pick an image when the image view is clicked and then save it in a custom folder.

First of all, here is my source code (in onCreate) :

    //Permissions
    //WRITE_EXTERNAL_STORAGE
    boolean hasPermission = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);

    if (!hasPermission) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_STORAGE);
    }

    //Events
    ivLogo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            createFolder("InspectionApp");

            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            File file = new File(Environment.getExternalStorageDirectory()
                    + File.separator
                    + "DCIM"
                    + File.separator
                    + "InspectionApp"
                    + File.separator
                    , "logo.jpg");

            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });

The "InspectionApp" folder is successfully created as a subfolder of android DCIM.

The gallery app opens on click, but the selected picture isn't saved as logo.jpg in my custom folder.

I don't get any error message and I know that onActivityResult is triggered by using logs.

I did something similar with ACTION_IMAGE_CAPTURE and it works like a charm, but ACTION_PICK doesn't :(

Thanks a lot for your help !

Aucun commentaire:

Enregistrer un commentaire