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

时间:2017-01-31 08:32:42

标签: javascript android cordova android-gallery photo-gallery

我需要打开图库才能在我的Android应用中选择图片。这是我的代码,它工作正常。

但是通过使用PHOTOLIBRARY,它将从设备的照片库中打开图像,并且使用SAVEDPHOTOALBUM将仅从设备的相机胶卷相册中选择图像 - 我可以在这里阅读https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/

我想打开我的应用程序特定文件夹而不是图库文件夹(例如:我创建一个名为'MYAPPIMAGES'的文件夹包含来自我的应用程序的图像,我想只显示'MYAPPIMAGES'文件夹中的图像,而不是库中的所有图像)。我怎样才能实现这种行为?有没有机会这样做?在此先感谢。

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

        }

2 个答案:

答案 0 :(得分:0)

该选项中允许的唯一值是:

Camera.PictureSourceType:枚举 定义Camera.getPicture调用的输出格式。注意:在iOS上传递PictureSourceType.PHOTOLIBRARY或PictureSourceType.SAVEDPHOTOALBUM以及DestinationType.NATIVE_URI将禁用因特定实现而导致的任何图像修改(调整大小,质量更改,裁剪等)。

种类:Camera的静态枚举属性 特性

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

答案 1 :(得分:0)

如果图库中的图像没有显示,而它们可能在中部显示404类型的位图,则这也可以解决您的问题。 请在您的图片中添加我代码中的所有标签,因为必须有一些元数据才能在图库中显示图片。 注意: 这对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;