在相机意图中显示图库

时间:2018-03-19 07:22:43

标签: android android-camera-intent

我正在开发一个功能,用户可以在其中拍照并从图库中选择。这基本上就是它开始的地方,然后继续将数据保存在数据库中。

 private void showPictureDialog(){
        AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
        pictureDialog.setTitle("Select Action");
        String[] pictureDialogItems = {
                "Select photo from gallery",
                "Capture photo from camera" };
        pictureDialog.setItems(pictureDialogItems,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                choosePhotoFromGallary();
                                break;
                            case 1:
                                takePhotoFromCamera();
                                break;
                        }
                    }
                });
        pictureDialog.show();
    }

但是,我想让用户体验更好。我想跳过用户选择其中一个选项(来自图库或相机)的对话框,而是以相机意图显示图库。类似的东西:

enter image description here

我希望你明白我的观点。谢谢:))

1 个答案:

答案 0 :(得分:0)

  1. 获取所有图片
  2. public List<File> getAllShownImagesPath(Context context) {
            //get all images
            String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.SIZE};
            List<File> result = new ArrayList<>();
            File f = null;
            final Cursor cursor = context.getContentResolver().
                    query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Specify the provider
                            columns, // The columns we're interested in
                            null, // A WHERE-filter query
                            null, // The arguments for the filter-query
                            MediaStore.Images.Media.DATE_ADDED + " DESC"
                    );
    
            if (cursor != null) {
                cursor.moveToFirst();
                for (int r = 0; r < cursor.getCount(); r++, cursor.moveToNext()) {
                    int i = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
                    //int l = cursor.getString(1).length();
                    final int image_path_col = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    if (i > 0) {
                        f = new File(cursor.getString(image_path_col));
                        if (f.length() > 0) {
                            result.add(f);
                        }
                    }
                }
                cursor.close();
            }
            return result;
        }

    1. 将所有图片添加到recyclerview或listview