Question

I have some problems which are puzzling to me. I seem unable to get the correct real path for a file based Uri. This is the code with end result variable values shown below it:

  String shareFile_Path = "";
  String contentURI_Path_Real = "";          
  String contentURI_Path_Simple = "";          
  Uri myContentUri_Camera = null;          

  Bitmap myBitmap = SharedCode.sharedGetImageAsBitmapFromResource(thisActivityContext, R.drawable.replace__logo__app_256_256);           
  File mySharedFile_Camera = SharedCode.sharedSaveBitmapToInternalStoragePath(thisActivityContext, "shared", "camera.jpg", myBitmap);

  if ( (mySharedFile_Camera != null) && (mySharedFile_Camera.exists()) ) {

    shareFile_Path = mySharedFile_Camera.getAbsolutePath();

    try {
      myContentUri_Camera = FileProvider.getUriForFile(thisActivityContext, "com.example.app.fileprovider", mySharedFile_Camera);
    }
    catch (Exception e) {
      String debug_s = e.toString();
    }            
    if (myContentUri_Camera != null) {
      contentURI_Path_Simple = myContentUri_Camera.getPath(); 
      contentURI_Path_Real = SharedCode.sharedGetRealPathFromURI(thisActivityContext, myContentUri_Camera);
    }  
  }  

End results of different variables:

  • sharedFile_Path = "/data/data/com.example.app/files/shared/camera.jpg"
  • contentURI_Path_Simple = "/providershared/camera.jpg"
  • contentURI_Path_Real = ""

As can be seen, the problem is the value for contentURI_Path_Real which is why the complete code for SharedCode.sharedGetRealPathFromURI is here - something I original found through SO:

  public static String sharedGetRealPathFromURI(Context context, Uri contentUri) {
    String res = "";
    if (contentUri != null) {      
      Cursor cursor = null;      
      try {
        cursor = context.getContentResolver().query(contentUri, null, null, null, null);
        if (cursor !== null) { 
          cursor.moveToFirst();
          int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
          if (idx > -1) {
            res = cursor.getString(idx);
          }  
        }  
      } 
      catch(Exception e){
      }         
      if (cursor != null) {
        cursor.close();
      }      
    }              
    return res;             
  }

When stepping through the code, it appears this if (idx > -1) yields false.

...

This is the relevant code for AndroidManifest.xml

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.app.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_filepaths" />
</provider>

This is the complete code for provider_filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path path="shared/" name="providershared" />
</paths>

...

In the end, I need a valid myContentUri_Camera for when calling a camera intent:

  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  if ( (myContentUri_Camera != null) && (contentURI_Path_Real != "") ) {
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myContentUri_Camera);
  }  
  startActivityForResult(cameraIntent, CAMERA_REQUEST); 

As it is now, if myContentUri_Camera is passed, the camera crashes the app when done - before onActivityResult returns. I suspect that is because it has no "real path" which is why I am asking about the path issue here.

Was it helpful?

Solution

In the end, I need a valid myContentUri_Camera for when calling a camera intent

You already have one.

However, you have your security messed up. Your <provider> is not exported and you are not using FLAG_GRANT_URI_WRITE_PERMISSION in your Intent. Hence, the camera app has no rights to work with your Uri. I suggest that you add FLAG_GRANT_URI_WRITE_PERMISSION.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top