Skip to content

Commit 28ed078

Browse files
committed
- address issue on hiddentao#65 and apply code changes base on hiddentao#65 (comment)
1 parent 7b8847e commit 28ed078

File tree

1 file changed

+64
-39
lines changed

1 file changed

+64
-39
lines changed

src/android/FilePath.java

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
122122
}
123123
}
124124

125+
125126
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
126-
for (int r:grantResults) {
127+
for (int r : grantResults) {
127128
if (r == PackageManager.PERMISSION_DENIED) {
128129
JSONObject resultObj = new JSONObject();
129130
resultObj.put("code", 3);
@@ -180,7 +181,7 @@ private static boolean isGooglePhotosUri(Uri uri) {
180181
private static boolean isGoogleDriveUri(Uri uri) {
181182
return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
182183
}
183-
184+
184185
/**
185186
* @param uri The Uri to check.
186187
* @return Whether the Uri authority is One Drive.
@@ -232,7 +233,7 @@ private static String getDataColumn(Context context, Uri uri, String selection,
232233
private static String getContentFromSegments(List<String> segments) {
233234
String contentPath = "";
234235

235-
for(String item : segments) {
236+
for (String item : segments) {
236237
if (item.startsWith("content://")) {
237238
contentPath = item;
238239
break;
@@ -275,6 +276,13 @@ private static String getPathFromExtSD(String[] pathData) {
275276
}
276277
}
277278

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+
278286
// Environment.isExternalStorageRemovable() is `true` for external and internal storage
279287
// so we cannot relay on it.
280288
//
@@ -290,7 +298,7 @@ private static String getPathFromExtSD(String[] pathData) {
290298
return fullPath;
291299
}
292300

293-
return fullPath;
301+
return "";
294302
}
295303

296304
/**
@@ -343,25 +351,38 @@ else if (isDownloadsDocument(uri)) {
343351
if (cursor != null && cursor.moveToFirst()) {
344352
String fileName = cursor.getString(0);
345353
String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
346-
if (!TextUtils.isEmpty(path)) {
354+
if (fileExists(path)) {
347355
return path;
348356
}
349357
}
350358
} finally {
351359
if (cursor != null)
352-
cursor.close();
360+
cursor.close();
353361
}
354362
//
355363
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+
}
359379

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();
364384
}
385+
365386
}
366387
// MediaProvider
367388
else if (isMediaDocument(uri)) {
@@ -376,35 +397,39 @@ else if (isMediaDocument(uri)) {
376397
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
377398
} else if ("audio".equals(type)) {
378399
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
400+
} else {
401+
contentUri = MediaStore.Files.getContentUri("external");
379402
}
380403

381404
final String selection = "_id=?";
382-
final String[] selectionArgs = new String[] {
405+
final String[] selectionArgs = new String[]{
383406
split[1]
384407
};
385408

386409
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);
390412
}
391413
}
392414
// MediaStore (and general)
393415
else if ("content".equalsIgnoreCase(uri.getScheme())) {
394416

395417
// Return the remote address
396418
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+
}
403428
}
404429
}
405430

406-
if(isGoogleDriveUri(uri) || isOneDriveUri(uri)){
407-
return getDriveFilePath(uri,context);
431+
if (isGoogleDriveUri(uri) || isOneDriveUri(uri)) {
432+
return getDriveFilePath(uri, context);
408433
}
409434

410435
return getDataColumn(context, uri, null, null);
@@ -417,26 +442,26 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
417442
return null;
418443
}
419444

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;
422447
Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
423448
/*
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+
* */
428453
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
429454
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
430455
returnCursor.moveToFirst();
431456
String name = (returnCursor.getString(nameIndex));
432457
String size = (Long.toString(returnCursor.getLong(sizeIndex)));
433-
File file = new File(context.getCacheDir(),name);
458+
File file = new File(context.getCacheDir(), name);
434459
try {
435460
InputStream inputStream = context.getContentResolver().openInputStream(uri);
436461
FileOutputStream outputStream = new FileOutputStream(file);
437462
int read = 0;
438463
int maxBufferSize = 1 * 1024 * 1024;
439-
int bytesAvailable = inputStream.available();
464+
int bytesAvailable = inputStream.available();
440465

441466
//int bufferSize = 1024;
442467
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
@@ -445,14 +470,14 @@ private static String getDriveFilePath(Uri uri,Context context){
445470
while ((read = inputStream.read(buffers)) != -1) {
446471
outputStream.write(buffers, 0, read);
447472
}
448-
Log.e("File Size","Size " + file.length());
473+
Log.e("File Size", "Size " + file.length());
449474
inputStream.close();
450475
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());
455480
}
456-
return file.getPath();
481+
return file.getPath();
457482
}
458483
}

0 commit comments

Comments
 (0)