@@ -122,8 +122,9 @@ else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
122
122
}
123
123
}
124
124
125
+
125
126
public void onRequestPermissionResult (int requestCode , String [] permissions , int [] grantResults ) throws JSONException {
126
- for (int r : grantResults ) {
127
+ for (int r : grantResults ) {
127
128
if (r == PackageManager .PERMISSION_DENIED ) {
128
129
JSONObject resultObj = new JSONObject ();
129
130
resultObj .put ("code" , 3 );
@@ -180,7 +181,7 @@ private static boolean isGooglePhotosUri(Uri uri) {
180
181
private static boolean isGoogleDriveUri (Uri uri ) {
181
182
return "com.google.android.apps.docs.storage" .equals (uri .getAuthority ()) || "com.google.android.apps.docs.storage.legacy" .equals (uri .getAuthority ());
182
183
}
183
-
184
+
184
185
/**
185
186
* @param uri The Uri to check.
186
187
* @return Whether the Uri authority is One Drive.
@@ -232,7 +233,7 @@ private static String getDataColumn(Context context, Uri uri, String selection,
232
233
private static String getContentFromSegments (List <String > segments ) {
233
234
String contentPath = "" ;
234
235
235
- for (String item : segments ) {
236
+ for (String item : segments ) {
236
237
if (item .startsWith ("content://" )) {
237
238
contentPath = item ;
238
239
break ;
@@ -275,6 +276,13 @@ private static String getPathFromExtSD(String[] pathData) {
275
276
}
276
277
}
277
278
279
+ //fix some devices(Android Q),'type' like "71F8-2C0A"
280
+ //but "primary".equalsIgnoreCase(type) is false
281
+ fullPath = "/storage/" + type + "/" + relativePath ;
282
+ if (fileExists (fullPath )) {
283
+ return fullPath ;
284
+ }
285
+
278
286
// Environment.isExternalStorageRemovable() is `true` for external and internal storage
279
287
// so we cannot relay on it.
280
288
//
@@ -290,7 +298,7 @@ private static String getPathFromExtSD(String[] pathData) {
290
298
return fullPath ;
291
299
}
292
300
293
- return fullPath ;
301
+ return "" ;
294
302
}
295
303
296
304
/**
@@ -343,25 +351,38 @@ else if (isDownloadsDocument(uri)) {
343
351
if (cursor != null && cursor .moveToFirst ()) {
344
352
String fileName = cursor .getString (0 );
345
353
String path = Environment .getExternalStorageDirectory ().toString () + "/Download/" + fileName ;
346
- if (! TextUtils . isEmpty (path )) {
354
+ if (fileExists (path )) {
347
355
return path ;
348
356
}
349
357
}
350
358
} finally {
351
359
if (cursor != null )
352
- cursor .close ();
360
+ cursor .close ();
353
361
}
354
362
//
355
363
final String id = DocumentsContract .getDocumentId (uri );
356
- try {
357
- final Uri contentUri = ContentUris .withAppendedId (
358
- Uri .parse ("content://downloads/public_downloads" ), Long .valueOf (id ));
364
+ String [] contentUriPrefixesToTry = new String []{
365
+ "content://downloads/public_downloads" ,
366
+ "content://downloads/my_downloads"
367
+ };
368
+
369
+ for (String contentUriPrefix : contentUriPrefixesToTry ) {
370
+ Uri contentUri = ContentUris .withAppendedId (Uri .parse (contentUriPrefix ), Long .valueOf (id ));
371
+ try {
372
+ String path = getDataColumn (context , contentUri , null , null );
373
+ if (path != null ) {
374
+ return path ;
375
+ }
376
+ } catch (Exception e ) {
377
+ }
378
+ }
359
379
360
- return getDataColumn ( context , contentUri , null , null );
361
- } catch ( NumberFormatException e ) {
362
- //In Android 8 and Android P the id is not a number
363
- return uri .getPath (). replaceFirst ( "^/document/raw:" , "" ). replaceFirst ( "^raw:" , "" ) ;
380
+ try {
381
+ return getDriveFilePath ( uri , context );
382
+ } catch ( Exception e ) {
383
+ return uri .getPath ();
364
384
}
385
+
365
386
}
366
387
// MediaProvider
367
388
else if (isMediaDocument (uri )) {
@@ -376,35 +397,39 @@ else if (isMediaDocument(uri)) {
376
397
contentUri = MediaStore .Video .Media .EXTERNAL_CONTENT_URI ;
377
398
} else if ("audio" .equals (type )) {
378
399
contentUri = MediaStore .Audio .Media .EXTERNAL_CONTENT_URI ;
400
+ } else {
401
+ contentUri = MediaStore .Files .getContentUri ("external" );
379
402
}
380
403
381
404
final String selection = "_id=?" ;
382
- final String [] selectionArgs = new String [] {
405
+ final String [] selectionArgs = new String []{
383
406
split [1 ]
384
407
};
385
408
386
409
return getDataColumn (context , contentUri , selection , selectionArgs );
387
- }
388
- else if (isGoogleDriveUri (uri )){
389
- return getDriveFilePath (uri ,context );
410
+ } else if (isGoogleDriveUri (uri )) {
411
+ return getDriveFilePath (uri , context );
390
412
}
391
413
}
392
414
// MediaStore (and general)
393
415
else if ("content" .equalsIgnoreCase (uri .getScheme ())) {
394
416
395
417
// Return the remote address
396
418
if (isGooglePhotosUri (uri )) {
397
- String contentPath = getContentFromSegments (uri .getPathSegments ());
398
- if (contentPath != "" ) {
399
- return getPath (context , Uri .parse (contentPath ));
400
- }
401
- else {
402
- return null ;
419
+ if (uri .toString ().contains ("mediakey" )) {
420
+ return getDriveFilePath (uri , context );
421
+ } else {
422
+ String contentPath = getContentFromSegments (uri .getPathSegments ());
423
+ if (contentPath != "" ) {
424
+ return getPath (context , Uri .parse (contentPath ));
425
+ } else {
426
+ return null ;
427
+ }
403
428
}
404
429
}
405
430
406
- if (isGoogleDriveUri (uri ) || isOneDriveUri (uri )){
407
- return getDriveFilePath (uri ,context );
431
+ if (isGoogleDriveUri (uri ) || isOneDriveUri (uri )) {
432
+ return getDriveFilePath (uri , context );
408
433
}
409
434
410
435
return getDataColumn (context , uri , null , null );
@@ -417,26 +442,26 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
417
442
return null ;
418
443
}
419
444
420
- private static String getDriveFilePath (Uri uri ,Context context ){
421
- Uri returnUri =uri ;
445
+ private static String getDriveFilePath (Uri uri , Context context ) {
446
+ Uri returnUri = uri ;
422
447
Cursor returnCursor = context .getContentResolver ().query (returnUri , null , null , null , null );
423
448
/*
424
- * Get the column indexes of the data in the Cursor,
425
- * * move to the first row in the Cursor, get the data,
426
- * * and display it.
427
- * */
449
+ * Get the column indexes of the data in the Cursor,
450
+ * * move to the first row in the Cursor, get the data,
451
+ * * and display it.
452
+ * */
428
453
int nameIndex = returnCursor .getColumnIndex (OpenableColumns .DISPLAY_NAME );
429
454
int sizeIndex = returnCursor .getColumnIndex (OpenableColumns .SIZE );
430
455
returnCursor .moveToFirst ();
431
456
String name = (returnCursor .getString (nameIndex ));
432
457
String size = (Long .toString (returnCursor .getLong (sizeIndex )));
433
- File file = new File (context .getCacheDir (),name );
458
+ File file = new File (context .getCacheDir (), name );
434
459
try {
435
460
InputStream inputStream = context .getContentResolver ().openInputStream (uri );
436
461
FileOutputStream outputStream = new FileOutputStream (file );
437
462
int read = 0 ;
438
463
int maxBufferSize = 1 * 1024 * 1024 ;
439
- int bytesAvailable = inputStream .available ();
464
+ int bytesAvailable = inputStream .available ();
440
465
441
466
//int bufferSize = 1024;
442
467
int bufferSize = Math .min (bytesAvailable , maxBufferSize );
@@ -445,14 +470,14 @@ private static String getDriveFilePath(Uri uri,Context context){
445
470
while ((read = inputStream .read (buffers )) != -1 ) {
446
471
outputStream .write (buffers , 0 , read );
447
472
}
448
- Log .e ("File Size" ,"Size " + file .length ());
473
+ Log .e ("File Size" , "Size " + file .length ());
449
474
inputStream .close ();
450
475
outputStream .close ();
451
- Log .e ("File Path" ,"Path " + file .getPath ());
452
- Log .e ("File Size" ,"Size " + file .length ());
453
- }catch (Exception e ){
454
- Log .e ("Exception" ,e .getMessage ());
476
+ Log .e ("File Path" , "Path " + file .getPath ());
477
+ Log .e ("File Size" , "Size " + file .length ());
478
+ } catch (Exception e ) {
479
+ Log .e ("Exception" , e .getMessage ());
455
480
}
456
- return file .getPath ();
481
+ return file .getPath ();
457
482
}
458
483
}
0 commit comments