如何从Environment.DIRECTORY_PICTURE中的目录中获取所有图像并显示在我的自定义画廊中

时间:2019-05-24 04:53:41

标签: android android-image android-gallery android-cursor

我正在为我的应用程序制作自定义应用程序厨房。从我的自定义相机捕获图像并将图像保存在Environment.DIRECTORY_PICTURES中的xyz目录中。我想访问所有这些图像并在自定义厨房recyclerview中显示。我尝试了很多方法但无法获取图像。谢谢,帮助我修复此功能。

   File fileX = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(), "xyz");
        projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

        Cursor cursor = getContentResolver().query(FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", fileX),
                projection, null, null,null);

 //            private final String[] projection = new String[]{ MediaStore.Images.Media.DISPLAY_NAME,  MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

        if (cursor == null) {
            message = handler.obtainMessage();
            message.what = commonVariables.ERROR;
            message.sendToTarget();
            return;
        }

        ArrayList<Image> temp = new ArrayList<>(cursor.getCount());
        File file;
        folders = new ArrayList<>();

        if (cursor.moveToLast()) {
            do {
                if (Thread.interrupted()) {
                    return;
                }

                 long id = cursor.getLong(cursor.getColumnIndexOrThrow(projection[0]));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(projection[1]));
                String path = cursor.getString(cursor.getColumnIndexOrThrow(projection[2]));
                String bucket = cursor.getString(cursor.getColumnIndexOrThrow(projection[3]));
                DebugLog.e("MSG: PATH = > " + name );
//                    file = new File(path);
//                    DebugLog.e("MSG: Absolute PATH = > " + file.getAbsolutePath());
//                    if (file.exists()) {
//                        Image image = new Image(id, name, path, false);
//                        temp.add(image);
//
//                        if (folderMode) {
//                            Folder folder = getFolder(bucket);
//                            if (folder == null) {
//                                folder = new Folder(bucket);
//                                folders.add(folder);
//                            }
//
//                            folder.getImages().add(image);
//                        }
//                    }

这给了我参数异常_ID不存在。

1 个答案:

答案 0 :(得分:1)

尝试以下解决方案:

private void getImages() {
    String uri = MediaStore.Images.Media.DATA;
    // if GetImageFromThisDirectory is the name of the directory from which image will be retrieved
    String condition = uri + " like '%/xyz/%'";
    String[] projection = {uri, MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.SIZE};
    try {
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                condition, null, null);
        if (cursor != null) {
            boolean isDataPresent = cursor.moveToFirst();
            if (isDataPresent) {
                do {
                    Log.e("ImagePath>>>", cursor.getString(cursor.getColumnIndex(uri)));
                } while (cursor.moveToNext());
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

不要忘记在您的AndroidManifest.xml中设置权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />