0

when I get file path of file in Android 11 .

protected File getOutputMediaFile() {
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, timeStamp + ".jpg");
    fileUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Log.e("File Uri Path", "Uri inserted into media store = " + fileUri);
   Toast.makeText(getActivity(), "File uri = "+fileUri, Toast.LENGTH_LONG).show();
    String path = getImageRealPathFromURI(fileUri);
    File file = new File(path);
    return file;
}

private String getImageRealPathFromURI(Uri contentUri) {
    String realPath = "";
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            realPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
        } else {
            Log.e("Image Real Path", "Cursor count appearing to be zero");
            Toast.makeText(getActivity(), "Cursor count appearing to be zero", Toast.LENGTH_LONG).show();
            realPath = "";
        }
    } catch (Exception e) {
        Log.e("Image Real Path", "Exception fetching getImageRealPathFromURI() due to " + e.toString());
       Toast.makeText(getActivity(),"Exception fetching getImageRealPathFromURI() due to "+e.toString(), Toast.LENGTH_LONG).show();
        realPath = "";
    } finally {
        cursor.close();
    }
    return realPath;
}

I meet error : /storage/emulated/0/Pictures/1614237849822.jpg: open failed: EEXIST (File exists). at libcore.io.IoBridge.open(IoBridge.java:492). When I getOutputMediaFile().getpath. Anyone help me?

Mr Robin
  • 53
  • 1
  • 6

3 Answers3

0

add this line in manifest file :

android:requestLegacyExternalStorage="true"

and make sure that you add Read external storage permission and also allow this permission

Muhammad Asad
  • 694
  • 6
  • 10
0

The getContentResolver().insert() will give you a nice fresh uri to write the content of a file to.

It gives an uri you can use. It does not create a file for you.

Even if you get the path of the yet non existing file with the .DATA column, that file does not exist yet.

You can simply check that with File.exists().

Only if you open an OutputStream for the obtained uri and write to it the file will be created.

Use the uri! For what do you need that data path?

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I want to setOutputFile in mediaRecorder. in Android 11, I am creating a file, which doesn't exist, but I get this error. – Harish Vats Jun 07 '21 at 10:36
0

You can use this type to set mMediaRecoder's path:

final ParcelFileDescriptor parcelFileDescriptor = mContext.getContentResolver().
                        openFileDescriptor(Uri.parse(mVideoProfile.path), "rw");

mMediaRecorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());

mVideoProfile.path = "content://media/external/video/media/751";
Antoine
  • 1,393
  • 4
  • 20
  • 26
zero
  • 1