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

Asked

Viewed 479 times

8

My application deals with writing and reading files, and I’m a little confused about how and when to use each of these types of Uri.

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

1 answer

5


What are:

  • File URI and Content URI sane Uniform Resource Identifier, identify a particular Resource.

  • Path, usually in the form of a string, identifies a path in a "file system(File System)". Indicates the path, through the folder hierarchy, to a file.

Which to use:

  • Any of them 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:, has to be a File Uri.

    • A Content Uri can identify an image in the gallery and be used to obtain the path even her.

      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;
      }
      
  • Applications target Android N(API level 24) cannot expose File URI’s out of the application. If a Intent contains a URI of this type FileUriExposedException.
    In that case a URI of the Content URI type should be used and a temporary access permission granted to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.