@@ -738,26 +738,34 @@ private int getErrorType(int infoType) {
738738 */
739739 private String convertUriToPath (Uri uri ) {
740740 String path = null ;
741- String scheme = uri .getScheme ();
742- if (null == scheme || scheme .equals ("" ) || scheme .equals (ContentResolver .SCHEME_FILE )) {
743- path = uri .getPath ();
744- } else if (scheme .equals (ContentResolver .SCHEME_CONTENT )) {
745- String [] projection = new String [] {MediaStore .MediaColumns .DATA };
746- Cursor cursor = null ;
747- try {
748- cursor = mContext .getContentResolver ().query (uri , projection , null , null , null );
749- } catch (SQLiteException e ) {
750- throw new IllegalArgumentException ("Given Uri is not formatted in a way " +
751- "so that it can be found in media store." );
752- }
753- if (null == cursor || 0 == cursor .getCount () || !cursor .moveToFirst ()) {
754- throw new IllegalArgumentException ("Given Uri could not be found in media store" );
741+ if (null != uri ) {
742+ String scheme = uri .getScheme ();
743+ if (null == scheme || scheme .equals ("" ) ||
744+ scheme .equals (ContentResolver .SCHEME_FILE )) {
745+ path = uri .getPath ();
746+ } else if (scheme .equals (ContentResolver .SCHEME_CONTENT )) {
747+ String [] projection = new String [] {MediaStore .MediaColumns .DATA };
748+ Cursor cursor = null ;
749+ try {
750+ cursor = mContext .getContentResolver ().query (uri , projection , null ,
751+ null , null );
752+ if (null == cursor || 0 == cursor .getCount () || !cursor .moveToFirst ()) {
753+ throw new IllegalArgumentException ("Given Uri could not be found" +
754+ " in media store" );
755+ }
756+ int pathIndex = cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .DATA );
757+ path = cursor .getString (pathIndex );
758+ } catch (SQLiteException e ) {
759+ throw new IllegalArgumentException ("Given Uri is not formatted in a way " +
760+ "so that it can be found in media store." );
761+ } finally {
762+ if (null != cursor ) {
763+ cursor .close ();
764+ }
765+ }
766+ } else {
767+ throw new IllegalArgumentException ("Given Uri scheme is not supported" );
755768 }
756- int pathIndex = cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .DATA );
757- path = cursor .getString (pathIndex );
758- cursor .close ();
759- } else {
760- throw new IllegalArgumentException ("Given Uri scheme is not supported" );
761769 }
762770 return path ;
763771 }
0 commit comments