What is the difference between File Uri, Content Uri and StringPath?

6

My application handles writing and reading files, and I'm a bit confused about how and when to use each of these types of Uri.

I think it would be best to understand the difference between these to find out when it would be the correct use of each.

    
asked by anonymous 28.03.2018 / 23:22

1 answer

4

What they are:

Which to use:

  • Any of these can be used to identify a file on the device. What determines the use of one or the other is the medium (api / class / method) you will use to get it.

  • Examples:

    • The class File has a constructor that receives a StringPath and another that receives a URI. However, the URI must have a scheme equal to file: , it must be a File Uri.

    • A Content Uri can identify an image in the gallery and be used to get the path to it .

      public String getImagePath(Uri contentUri) {
          String[] campos = { MediaStore.Images.Media.DATA };
          Cursor cursor = getContentResolver().query(contentUri, campos, null, null, null);
          cursor.moveToFirst();
          String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
          cursor.close();
          return path;
      }
      
  • Android target applications (API level 24) will not be able to expose File URI's out of the application. If an intent contains a URI of this type, a FileUriExposedException will be cast.
    In this case , a URI of type Content URI should be used and grant a temporary access permission.

29.03.2018 / 13:32