@@ -64,7 +64,7 @@ protected void onCreate(final Bundle savedInstanceState) {
64
64
if (getResources ().getBoolean (R .bool .portrait_only )) { //Portrait only on phone (also landscape on tablet)
65
65
setRequestedOrientation (ActivityInfo .SCREEN_ORIENTATION_PORTRAIT );
66
66
}
67
- listView = ( ListView ) findViewById (android .R .id .list );
67
+ listView = findViewById (android .R .id .list );
68
68
setRecent = new HashSet <>();
69
69
setRecent = getPreferences (MODE_PRIVATE ).getStringSet ("recent" , null );
70
70
if (setRecent != null ) {
@@ -104,9 +104,9 @@ public void onClick(DialogInterface dialog, int id) {
104
104
return true ;
105
105
}
106
106
});
107
- editText = ( EditText ) findViewById (R .id .editTextPath );
107
+ editText = findViewById (R .id .editTextPath );
108
108
editText .setText (getPreferences (MODE_PRIVATE ).getString ("path" , DEFAULT_PATH ));
109
- final ImageButton buttonStorage = ( ImageButton ) findViewById (R .id .buttonStorage );
109
+ final ImageButton buttonStorage = findViewById (R .id .buttonStorage );
110
110
buttonStorage .setOnClickListener (new View .OnClickListener () {
111
111
@ Override
112
112
public void onClick (final View v ) {
@@ -119,7 +119,7 @@ public void onClick(final View v) {
119
119
}
120
120
});
121
121
122
- final Button buttonOpen = ( Button ) findViewById (R .id .buttonOpen );
122
+ final Button buttonOpen = findViewById (R .id .buttonOpen );
123
123
buttonOpen .setOnClickListener (new View .OnClickListener () {
124
124
@ Override
125
125
public void onClick (final View v ) {
@@ -141,7 +141,7 @@ public void onClick(final View v) {
141
141
}
142
142
});
143
143
144
- final Button buttonCreate = ( Button ) findViewById (R .id .buttonCreate );
144
+ final Button buttonCreate = findViewById (R .id .buttonCreate );
145
145
buttonCreate .setOnClickListener (new View .OnClickListener () {
146
146
@ Override
147
147
public void onClick (final View v ) {
@@ -272,9 +272,11 @@ public boolean onOptionsItemSelected(final MenuItem item) {
272
272
protected void onActivityResult (final int requestCode , final int resultCode , final Intent intent ) {
273
273
if (resultCode == RESULT_OK ) {
274
274
final Uri uri = intent .getData ();
275
- final String path = convertMediaUriToPath (uri );
276
- final EditText editText = (EditText ) findViewById (R .id .editTextPath );
277
- editText .setText (path );
275
+ if (uri != null ) {
276
+ final String path = convertMediaUriToPath (uri );
277
+ final EditText editText = findViewById (R .id .editTextPath );
278
+ editText .setText (path );
279
+ }
278
280
}
279
281
}
280
282
@@ -285,20 +287,27 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
285
287
* @return path of the file
286
288
*/
287
289
private String convertMediaUriToPath (final Uri uri ) {
288
- if (uri .toString ().substring (0 , 4 ).equals ("file" )) {
289
- //remove "file://" from the uri
290
- return uri .toString ().substring (7 );
291
- } else if (uri .toString ().substring (0 , 7 ).equals ("content" )) {
292
- final String [] project = {MediaStore .Files .FileColumns .DATA };
293
- final Cursor cursor = getContentResolver ().query (uri , project , null , null , null );
294
- final int columnIndex = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
295
- cursor .moveToFirst ();
296
- final String path = cursor .getString (columnIndex );
297
- cursor .close ();
298
- return path ;
290
+ final String sUri = uri .toString ();
291
+ final int minLenght = "file://" .length ();
292
+ String path = "" ;
293
+
294
+ //uri should always begin with "file" or "content"
295
+ if (sUri .length () > minLenght ) {
296
+ if (sUri .substring (0 , 4 ).equals ("file" )) {
297
+ //remove "file://" from the uri
298
+ path = uri .toString ().substring (7 );
299
+ } else if (uri .toString ().substring (0 , 7 ).equals ("content" )) {
300
+ final String [] project = {MediaStore .Files .FileColumns .DATA };
301
+ final Cursor cursor = getContentResolver ().query (uri , project , null , null , null );
302
+ if (cursor != null ) {
303
+ final int columnIndex = cursor .getColumnIndexOrThrow (MediaStore .Images .Media .DATA );
304
+ cursor .moveToFirst ();
305
+ path = cursor .getString (columnIndex );
306
+ cursor .close ();
307
+ }
308
+ }
299
309
}
300
- //uri will always begin with file or content, but with this is handled even if uri have something weird
301
- return "" ;
310
+ return path ;
302
311
}
303
312
304
313
private void updateRecentList (String item , boolean isDelete ) {
0 commit comments