Get filepath and filename of selected gallery image in Android

后端 未结 9 2095
天命终不由人
天命终不由人 2020-11-27 04:47

I am creating an app which uploads a selected image from the gallery and uploads it to a web service. The webservice requires the filename of selected image plus a base64 e

9条回答
  •  自闭症患者
    2020-11-27 05:45

    Try this:

    Uri selectedImageURI = data.getData();
    imageFile = new File(getRealPathFromURI(selectedImageURI));
    

    And

        private String getRealPathFromURI(Uri contentURI) {
        Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            return contentURI.getPath();
        } else { 
            cursor.moveToFirst(); 
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
            return cursor.getString(idx); 
        }
    }
    

提交回复
热议问题