maifenku
maifenku

Reputation: 11

cordova-plugin-camera : how to open images in specific folder instead of showing all images from gallery

I need to open gallery to select image in my android app. Here's my code and it works fine.

But by using PHOTOLIBRARY, it will open image from the device's photo libraryand by using SAVEDPHOTOALBUM will chose image only from the device's Camera Roll album - as i can read here https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/

I want to open my app specific folder instead of gallery folder (ex: i create a folder called 'MYAPPIMAGES' contains images from my app and i want to show only images from 'MYAPPIMAGES' folder, not all of images in gallery). How can I achieve this behaviour? Is there any chance to do that? Thanks in Advance.

var picOptions = {
        destinationType: navigator.camera.DestinationType.FILE_URI,
        quality: 80,
        targetWidth: 800,
        targetHeight: 800,
        maximumImagesCount: 5,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
    };

    $cordovaImagePicker.getPictures(picOptions).then(function (imageURI) {

        for (var i = 0; i < imageURI.length; i++) {
            var str = imageURI[i];
            var n = str.lastIndexOf('/');
            var splitStr = str.substring(n+1);
            var dir = str.substring(0,n+1);
            console.log(imageURI[i]+' '+splitStr+' '+dir);
            convert64(imageURI[i], splitStr, dir); 

        }

Upvotes: 1

Views: 3047

Answers (2)

Mubashir Murtaza
Mubashir Murtaza

Reputation: 337

This will aslo solve your problem if your image in gallery are not showing instead they might showing a 404 type bitmap in midle. please add all the tags that are in my code with your image because there must some meta data in order to show image in gallery. NOTE: This is valid for Android 9 and below ,For Android Q, you can use this tag with android Q to store you img in specific folder in gallery values.put(MediaStore.Images.Media.RELATIVE_PATH, "yourfoldernaeme");

      String resultPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ 
      getString(R.string.directory) + System.currentTimeMillis() + ".jpg";

       new File(resultPath).getParentFile().mkdir();

        try {
            OutputStream fileOutputStream = new FileOutputStream(resultPath);
            savedBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        savedBitmap.recycle();

        File file = new File(resultPath);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "Photo");
        values.put(MediaStore.Images.Media.DESCRIPTION, "Edited");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
        values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
        values.put("_data", resultPath);

        ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);




        return  resultPath;

Upvotes: 0

Mikel Sanchez
Mikel Sanchez

Reputation: 2890

The only values allowed in that options are:

Camera.PictureSourceType : enum Defines the output format of Camera.getPicture call. Note: On iOS passing PictureSourceType.PHOTOLIBRARY or PictureSourceType.SAVEDPHOTOALBUM along with DestinationType.NATIVE_URI will disable any image modifications (resize, quality change, cropping, etc.) due to implementation specific.

Kind: static enum property of Camera Properties

Name            Type    Default Description
PHOTOLIBRARY    number  0       Choose image from the device's photo library (same as SAVEDPHOTOALBUM for Android)

CAMERA          number  1       Take picture from camera

SAVEDPHOTOALBUM number  2       Choose image only from the device's Camera Roll album (same as PHOTOLIBRARY for Android)

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/#module_camera.CameraOptions

Upvotes: 1

Related Questions