Android getContentResolver or the developer-crossword-puzzle

So I have a question to whoever though that the Android API getContentResolver is a good idea and easily usable by developers.

What the heck were you thinking dude ?

I have spent two full days trying to figure this one out and it still escapes mas as to how I can share files between applications.

I have clicked and read all of the first two pages of my google searches in various forms to no avail. and thus I conclude that this thing that should be as simple as

  String mimeType = MimeTypes.getMimeType ( file.getName ( ) );
  Uri uri = FileProvider.shareFile ( file );
  intent.setDataAndType ( uri, mimeType );
  startActivity ( intent );

and on the receiving side

  String path = context.getContentResolver ( ).getFilePath ( uri );

is marred in a lot of confusing obstructions and hard to understand reasoning. Take for example the code you will find everywhere online

 cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);

Then you find references to query, tables records rows, and columns. That may be great for sharing a DB however it does not resolve file sharing, however that is the code you have to fight with.

Of course cursor is mostly null and in cases it is not null it will throw an exception when you try to execute the above query.

Now lets assume you get through this and you have a valid cursor object. now this piece of code fails you

  if ( cursor.moveToFirst ( ) )
    path = cursor.getString ( 0 );

So what does this all mean ? For me it resulted in two very frustrating days of lost productivity and head-scratching why such a simple task is so unnecessarily complex.
Google: There is no excuse for this complexity.

Here is the final code to retrieve the file

  if ( "content".equalsIgnoreCase ( uri.getScheme ( ) ) ) {
    Cursor cursor = null;
    try {
      String[] proj = { MediaStore.Images.Media.DATA };
      try {
        cursor = context.getContentResolver().query ( uri, proj, null, null, null );
        if ( cursor != null ) {
          try {
            int column_index = cursor.getColumnIndexOrThrow ( MediaStore.Images.Media.DATA );
            if (cursor.moveToFirst ( ) )
              path = cursor.getString ( column_index );
          }
          catch ( IllegalArgumentException e )  {
            e.printStackTrace ( );
          }
        }
      }
      catch ( Exception e )  {
        e.printStackTrace ( );
      }
    }
    finally {
      if ( cursor != null ) {
           cursor.close ( );
      }
    }
    String str = path;
    if (str.indexOf("/file/") == 0)
        str = str.replace("/file", "");
    return str;
  }
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s