Get/pick an image from Android's built-in Gallery app programmatically

前端 未结 19 1186
终归单人心
终归单人心 2020-11-22 00:49

I am trying to open an image / picture in the Gallery built-in app from inside my application.

I have a URI of the picture (the picture is located on the SD card).

相关标签:
19条回答
  • 2020-11-22 01:26

    Below solution work for 2.3(Gingerbread)-4.4(Kitkat), 5.0(Lollipop) and 6.0(Marshmallow) also:-

    Step 1 Code for opening the gallery to select pics:

    public static final int PICK_IMAGE = 1;
    private void takePictureFromGalleryOrAnyOtherFolder() 
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    }
    

    Step 2 Code for getting data in onActivityResult:

     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK) {
                   if (requestCode == PICK_IMAGE) {
                        Uri selectedImageUri = data.getData();
                        String imagePath = getRealPathFromURI(selectedImageUri);
                       //Now you have imagePath do whatever you want to do now
                     }//end of inner if
                 }//end of outer if
          }
    
     public String getRealPathFromURI(Uri contentUri) {
            //Uri contentUri = Uri.parse(contentURI);
    
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = null;
            try {
                if (Build.VERSION.SDK_INT > 19) {
                    // Will return "image:x*"
                    String wholeID = DocumentsContract.getDocumentId(contentUri);
                    // Split at colon, use second item in the array
                    String id = wholeID.split(":")[1];
                    // where id is equal to
                    String sel = MediaStore.Images.Media._ID + "=?";
    
                    cursor = context.getContentResolver().query(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            projection, sel, new String[] { id }, null);
                } else {
                    cursor = context.getContentResolver().query(contentUri,
                            projection, null, null, null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            String path = null;
            try {
                int column_index = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                path = cursor.getString(column_index).toString();
                cursor.close();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            return path;
        }
    
    0 讨论(0)
提交回复
热议问题