From 6d999ff9330fd930f59b712cdc834d3e72f6e751 Mon Sep 17 00:00:00 2001 From: maxiaohong Date: Sun, 23 Sep 2018 22:02:38 +0800 Subject: [PATCH] fix get file path from uri error --- app/build.gradle | 4 +- .../ui/activity/BrowserActivity.java | 61 ++++----- .../seafile/seadroid2/util/UriFilePath.java | 128 ------------------ 3 files changed, 31 insertions(+), 162 deletions(-) delete mode 100644 app/src/main/java/com/seafile/seadroid2/util/UriFilePath.java diff --git a/app/build.gradle b/app/build.gradle index 6a923985c..c5575f89a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId 'com.seafile.seadroid2' minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 82 - versionName "2.2.7" + versionCode 83 + versionName "2.2.8" multiDexEnabled true resValue "string", "authorities", applicationId + '.cameraupload.provider' resValue "string", "account_type", "com.seafile.seadroid2.account.api2" diff --git a/app/src/main/java/com/seafile/seadroid2/ui/activity/BrowserActivity.java b/app/src/main/java/com/seafile/seadroid2/ui/activity/BrowserActivity.java index 18f4e6939..4052a93b2 100644 --- a/app/src/main/java/com/seafile/seadroid2/ui/activity/BrowserActivity.java +++ b/app/src/main/java/com/seafile/seadroid2/ui/activity/BrowserActivity.java @@ -91,15 +91,18 @@ import com.seafile.seadroid2.ui.fragment.ReposFragment; import com.seafile.seadroid2.ui.fragment.StarredFragment; import com.seafile.seadroid2.util.ConcurrentAsyncTask; -import com.seafile.seadroid2.util.UriFilePath; import com.seafile.seadroid2.util.Utils; import com.seafile.seadroid2.util.UtilsJellyBean; import com.viewpagerindicator.IconPagerAdapter; +import org.apache.commons.io.IOUtils; import org.json.JSONException; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -1381,37 +1384,31 @@ protected File[] doInBackground(Uri... uriList) { List fileList = new ArrayList(); for (Uri uri: uriList) { // Log.d(DEBUG_TAG, "Uploading file from uri: " + uri); -// ============ copy file to seafile dir =================== -// InputStream in = null; -// OutputStream out = null; -// -// try { -// File tempDir = DataManager.createTempDir(); -// File tempFile = new File(tempDir, Utils.getFilenamefromUri(BrowserActivity.this, uri)); -// -// if (!tempFile.createNewFile()) { -// throw new RuntimeException("could not create temporary file"); -// } -// -// in = getContentResolver().openInputStream(uri); -// out = new FileOutputStream(tempFile); -// IOUtils.copy(in, out); -// -// fileList.add(tempFile); -// -// } catch (IOException e) { -// Log.d(DEBUG_TAG, "Could not open requested document", e); -// } catch (RuntimeException e) { -// Log.d(DEBUG_TAG, "Could not open requested document", e); -// } finally { -// IOUtils.closeQuietly(in); -// IOUtils.closeQuietly(out); -// } -// =============== new idea,file upload no copy seafile dir ================ - String path = UriFilePath.getFileAbsolutePath(BrowserActivity.this, uri); - File tempFile = new File(path); - fileList.add(tempFile); -// =============== new idea,file upload no copy seafile dir ================ + InputStream in = null; + OutputStream out = null; + + try { + File tempDir = DataManager.createTempDir(); + File tempFile = new File(tempDir, Utils.getFilenamefromUri(BrowserActivity.this, uri)); + + if (!tempFile.createNewFile()) { + throw new RuntimeException("could not create temporary file"); + } + + in = getContentResolver().openInputStream(uri); + out = new FileOutputStream(tempFile); + IOUtils.copy(in, out); + + fileList.add(tempFile); + + } catch (IOException e) { + Log.d(DEBUG_TAG, "Could not open requested document", e); + } catch (RuntimeException e) { + Log.d(DEBUG_TAG, "Could not open requested document", e); + } finally { + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(out); + } } return fileList.toArray(new File[]{}); } diff --git a/app/src/main/java/com/seafile/seadroid2/util/UriFilePath.java b/app/src/main/java/com/seafile/seadroid2/util/UriFilePath.java deleted file mode 100644 index 089027a3d..000000000 --- a/app/src/main/java/com/seafile/seadroid2/util/UriFilePath.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.seafile.seadroid2.util; - -import android.annotation.TargetApi; -import android.app.Activity; -import android.content.ContentUris; -import android.content.Context; -import android.database.Cursor; -import android.net.Uri; -import android.os.Environment; -import android.provider.DocumentsContract; -import android.provider.MediaStore; -import android.text.TextUtils; - -/** - * @Description: [ get file path wiht Uri ] - * @Author: [ Saud ] - * @CreateDate: [ 2018/1/20 17:06 ] - * @UpDate: [ 2018/1/20 17:06 ] - * @Version: [ v1.0 ] - */ - -public class UriFilePath { - - @TargetApi(19) - public static String getFileAbsolutePath(Activity context, Uri fileUri) { - if (context == null || fileUri == null) - return null; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, - fileUri)) { - if (isExternalStorageDocument(fileUri)) { - String docId = DocumentsContract.getDocumentId(fileUri); - String[] split = docId.split(":"); - String type = split[0]; - if ("primary".equalsIgnoreCase(type)) { - return Environment.getExternalStorageDirectory() + "/" + split[1]; - } - } else if (isDownloadsDocument(fileUri)) { - String id = DocumentsContract.getDocumentId(fileUri); - if (!TextUtils.isEmpty(id)) { - if (id.startsWith("raw:")) { - return id.replaceFirst("raw:", ""); - } - try { - Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - return getDataColumn(context, contentUri, null, null); - } catch (NumberFormatException e) { - return null; - } - } - return null; - } else if (isMediaDocument(fileUri)) { - String docId = DocumentsContract.getDocumentId(fileUri); - String[] split = docId.split(":"); - String type = split[0]; - Uri contentUri = null; - if ("image".equals(type)) { - contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; - } else if ("video".equals(type)) { - contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; - } else if ("audio".equals(type)) { - contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; - } - String selection = MediaStore.Images.Media._ID + "=?"; - String[] selectionArgs = new String[]{split[1]}; - return getDataColumn(context, contentUri, selection, selectionArgs); - } - } // MediaStore (and general) - else if ("content".equalsIgnoreCase(fileUri.getScheme())) { - // Return the remote address - if (isGooglePhotosUri(fileUri)) - return fileUri.getLastPathSegment(); - return getDataColumn(context, fileUri, null, null); - } - // File - else if ("file".equalsIgnoreCase(fileUri.getScheme())) { - return fileUri.getPath(); - } - return null; - } - - public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { - Cursor cursor = null; - String[] projection = {MediaStore.Images.Media.DATA}; - try { - cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); - if (cursor != null && cursor.moveToFirst()) { - int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - return cursor.getString(index); - } - } finally { - if (cursor != null) - cursor.close(); - } - return null; - } - - /** - * @param uri The Uri to check. - * @return Whether the Uri authority is ExternalStorageProvider. - */ - public static boolean isExternalStorageDocument(Uri uri) { - return "com.android.externalstorage.documents".equals(uri.getAuthority()); - } - - /** - * @param uri The Uri to check. - * @return Whether the Uri authority is DownloadsProvider. - */ - public static boolean isDownloadsDocument(Uri uri) { - return "com.android.providers.downloads.documents".equals(uri.getAuthority()); - } - - /** - * @param uri The Uri to check. - * @return Whether the Uri authority is MediaProvider. - */ - public static boolean isMediaDocument(Uri uri) { - return "com.android.providers.media.documents".equals(uri.getAuthority()); - } - - /** - * @param uri The Uri to check. - * @return Whether the Uri authority is Google Photos. - */ - public static boolean isGooglePhotosUri(Uri uri) { - return "com.google.android.apps.photos.content".equals(uri.getAuthority()); - } -}