From 26028d90ca7ab371df7a09234a52c08d1be25b27 Mon Sep 17 00:00:00 2001 From: XFY9326 <1069665464@qq.com> Date: Tue, 10 Dec 2019 16:43:30 +0800 Subject: [PATCH] Fix: support old app in sandbox storage on android Q Fix: uri in return intent may not have read permission Fix: other problems --- app/build.gradle | 4 +-- app/src/main/AndroidManifest.xml | 3 ++- .../xfy9326/selectmedia/MainActivity.java | 18 ++++++------- .../selectmedia/TransferMediaAsync.java | 25 ++++++++----------- app/src/main/res/values-zh/strings.xml | 2 -- app/src/main/res/values/strings.xml | 4 +-- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2a2ae71..7c58ae2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "tool.xfy9326.selectmedia" minSdkVersion 14 targetSdkVersion 29 - versionCode 5 - versionName "1.2.2" + versionCode 6 + versionName "1.2.3" buildTypes { release { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6bf87e..e3dc626 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,8 +9,9 @@ + tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"> { if (isSuccess) { - exitWithResult(mediaUri); + exitWithResult(resultIntent); } else { Toast.makeText(MainActivity.this, R.string.media_file_transfer_error, Toast.LENGTH_SHORT).show(); onBackPressed(); } - }).execute(mediaUri, outputMediaUri); + }).execute(resultIntent.getData(), outputMediaUri); } } - private void exitWithResult(Uri mediaUri) { - setResult(RESULT_OK, new Intent().setData(mediaUri)); + private void exitWithResult(Intent resultIntent) { + setResult(RESULT_OK, resultIntent); finish(); } diff --git a/app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java b/app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java index 97a5606..c4578ba 100644 --- a/app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java +++ b/app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java @@ -1,13 +1,13 @@ package tool.xfy9326.selectmedia; import android.content.ContentResolver; -import android.content.res.AssetFileDescriptor; import android.net.Uri; import android.os.AsyncTask; import androidx.annotation.NonNull; import java.nio.channels.FileChannel; +import java.util.Objects; class TransferMediaAsync extends AsyncTask { private static final String FILE_MODE_READ = "r"; @@ -23,25 +23,22 @@ class TransferMediaAsync extends AsyncTask { @Override protected Boolean doInBackground(Uri... uris) { if (uris.length >= 2) { - try (AssetFileDescriptor inputFileDescriptor = resolver.openAssetFileDescriptor(uris[0], FILE_MODE_READ); - AssetFileDescriptor outputFileDescriptor = resolver.openAssetFileDescriptor(uris[1], FILE_MODE_WRITE)) { - if (inputFileDescriptor != null && outputFileDescriptor != null) { - try (FileChannel inputChannel = inputFileDescriptor.createInputStream().getChannel(); - FileChannel outputChannel = outputFileDescriptor.createOutputStream().getChannel()) { - long size = inputChannel.size(); - while (size > 0) { - long count = outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); - if (count > 0) { - size -= count; - } - } + Uri mediaUri = uris[0]; + Uri mediaOutputUri = uris[1]; + try (FileChannel inputChannel = Objects.requireNonNull(resolver.openAssetFileDescriptor(mediaUri, FILE_MODE_READ)).createInputStream().getChannel(); + FileChannel outputChannel = Objects.requireNonNull(resolver.openAssetFileDescriptor(mediaOutputUri, FILE_MODE_WRITE)).createOutputStream().getChannel()) { + long size = inputChannel.size(); + while (size > 0) { + long count = outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); + if (count > 0) { + size -= count; } } + return true; } catch (Exception e) { e.printStackTrace(); return false; } - return true; } return false; } diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 0864a7e..7992b97 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -3,8 +3,6 @@ 选择媒体 从文件管理器选择媒体 从相册选择媒体 - 正在加载中…… - 正在转移数据……\n路径:%1$s 权限授予失败! 未选择任何媒体文件! 媒体文件传输错误! diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 20917b2..f1e6092 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,10 +3,8 @@ Select Media Select media from file explorer Select media from album - Loading … … - Transforming data now … …\nPath: %1$s Permission grant failed! - Select no media file! + No media file selected! Media file transfer error!