0

for getting the real path of capture image and gallery image the following code work perfectly fine for me.

private String getRealPathFromURI(Uri contentUri) {
        String result = null;
        try{
            String[] proj = { MediaStore.Images.Media.DATA };
            CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
            Cursor cursor = loader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
            cursor.close();
        }catch(Exception e){
            Log.i("error_getting_path",e.getMessage());
        }

        return result;
    }

but in android 10 and above device i am not able to get the path of capture and gallery image . so my app is not working in android 10 and above for this feature. please suggest the best way to get path in all android devices.

  • The .DATA column is not used on Android 10 and 11. The best way is not try to get a path from a nice uri (a nasty habbit anyhow) but to use the uri directly. You dont need a path... well.. mostly not. – blackapps Sep 23 '20 at 12:12
  • File file = new File(getRealPathFromURI(contentUri)); RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("image",file.getName(),requestFile) .build(); – user9437108 Sep 23 '20 at 12:46
  • this is my approact to upload the file using path of selected or capture image how can i do this without having the path of file. file creating from uri is not working for .. @blackapps – user9437108 Sep 23 '20 at 12:46
  • There is something like .addStreamUri..... Sorry forgot the name. Have a look. The ide gives you a choose. – blackapps Sep 23 '20 at 13:36

1 Answers1

1
  private static String getRealPathFromURI(Uri uri) {
    Uri returnUri = uri;
    Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
    int nameIndex = returnCursor.getColumnIndex( OpenableColumns.DISPLAY_NAME);

    returnCursor.moveToFirst();
    String name = (returnCursor.getString(nameIndex));

    File file = new File(context.getFilesDir(), name);
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;
        int maxBufferSize = 1 * 1024 * 1024;
        int bytesAvailable = inputStream.available();

      
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);

        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }
      
        inputStream.close();
        outputStream.close();
        Log.e("File Path", "Path " + file.getAbsolutePath());

    } catch (Exception e) {
        Log.e("Exception", e.getMessage());
    }
    return file.getAbsolutePath();
}
Sunil Chaudhary
  • 1,221
  • 12
  • 25