@@ -32,26 +32,31 @@ public class OpenerActivity extends Activity {
32
32
public static String TAG = "OpenerActivity" ;
33
33
34
34
public static String findFileInDownloads (Context context , String name , String id ) {
35
- ContentResolver contentResolver = context .getContentResolver ();
36
- Uri uri = MediaStore .Files .getContentUri ("external" );
35
+ try {
36
+ ContentResolver contentResolver = context .getContentResolver ();
37
+ Uri uri = MediaStore .Files .getContentUri ("external" );
37
38
38
- String [] projection = {
39
- MediaStore .Files .FileColumns .DATA
40
- };
39
+ String [] projection = {
40
+ MediaStore .Files .FileColumns .DATA
41
+ };
41
42
42
- String selection = MediaStore .Files .FileColumns ._ID + " = ? AND " +
43
- MediaStore .Files .FileColumns .DISPLAY_NAME + " = ?" ;
43
+ String selection = MediaStore .Files .FileColumns ._ID + " = ? AND " +
44
+ MediaStore .Files .FileColumns .DISPLAY_NAME + " = ?" ;
44
45
45
- String [] selectionArgs = new String []{id , name };
46
+ String [] selectionArgs = new String []{id , name };
46
47
47
- try (Cursor cursor = contentResolver .query (uri , projection , selection , selectionArgs , null )) {
48
- if (cursor != null && cursor .moveToFirst ()) {
49
- int filePathId = cursor .getColumnIndex (MediaStore .Files .FileColumns .DATA );
50
- String filePath = cursor .getString (filePathId );
51
- LOG .d (TAG , "FileFinder" , "File Path: " + filePath );
52
- return filePath ;
48
+ try (Cursor cursor = contentResolver .query (uri , projection , selection , selectionArgs , null )) {
49
+ if (cursor != null && cursor .moveToFirst ()) {
50
+ int filePathId = cursor .getColumnIndex (MediaStore .Files .FileColumns .DATA );
51
+ String filePath = cursor .getString (filePathId );
52
+ LOG .d (TAG , "FileFinder" , "File Path: " + filePath );
53
+ return filePath ;
54
+ }
53
55
}
56
+ } catch (Exception e ) {
57
+ LOG .e (e );
54
58
}
59
+
55
60
return null ;
56
61
}
57
62
@@ -131,12 +136,18 @@ protected void onCreate(Bundle savedInstanceState) {
131
136
}
132
137
if (!file .isFile ()) {
133
138
LOG .d (TAG , "Find file in getDataPath /indexOf 1" );
134
- file = new File (dataPath .substring (dataPath .indexOf ("/" , 1 )));
139
+ int beginIndex = dataPath .indexOf ("/" , 1 );
140
+ if (beginIndex > 0 ) {
141
+ file = new File (dataPath .substring (beginIndex ));
142
+ }
135
143
LOG .d (TAG , "File:" , file .getPath ());
136
144
}
137
145
if (!file .isFile ()) {
138
146
LOG .d (TAG , "Find file in getDataPath /indexOf 2" );
139
- file = new File (dataPath .substring (dataPath .indexOf ("/" , 1 )));
147
+ int beginIndex = dataPath .indexOf ("/" , 1 );
148
+ if (beginIndex > 0 ) {
149
+ file = new File (dataPath .substring (beginIndex ));
150
+ }
140
151
LOG .d (TAG , "File:" , file .getPath ());
141
152
}
142
153
0 commit comments