简体   繁体   English

cordova-plugin-camera:如何在特定文件夹中打开图像而不是显示图库中的所有图像

[英]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.我需要打开图库以在我的 android 应用程序中选择图像。 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/但是通过使用 PHOTOLIBRARY,它将从设备的照片库中打开图像,并且通过使用 SAVEDPHOTOALBUM 将仅从设备的相机胶卷相册中选择图像 - 我可以在这里阅读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).我想打开我的应用程序特定文件夹而不是图库文件夹(例如:我创建了一个名为“MYAPPIMAGES”的文件夹,其中包含来自我的应用程序的图像,我只想显示“MYAPPIMAGES”文件夹中的图像,而不是图库中的所有图像)。 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); 

        }

The only values allowed in that options are:该选项中允许的唯一值是:

Camera.PictureSourceType : enum Defines the output format of Camera.getPicture call. Camera.PictureSourceType : enum 定义 Camera.getPicture 调用的输出格式。 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.注意:在 iOS 上传递 PictureSourceType.PHOTOLIBRARY 或 PictureSourceType.SAVEDPHOTOALBUM 以及 DestinationType.NATIVE_URI 将禁用由于特定于实现的任何图像修改(调整大小、质量更改、裁剪等)。

Kind: static enum property of Camera Properties Kind: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 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/#module_camera.CameraOptions

This will aslo solve your problem if your image in gallery are not showing instead they might showing a 404 type bitmap in midle.如果您在图库中的图像没有显示,而它们可能会在 midle 中显示 404 类型的位图,这也将解决您的问题。 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");注意:这适用于 Android 9 及以下版本,对于 Android Q,您可以使用此标签与 android Q 将您的 img 存储在画廊 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM