Skip to content
This repository has been archived by the owner on Nov 28, 2021. It is now read-only.

Commit

Permalink
Fix: support old app in sandbox storage on android Q
Browse files Browse the repository at this point in the history
Fix: uri in return intent may not have read permission
Fix: other problems
  • Loading branch information
XFY9326 committed Dec 10, 2019
1 parent c1d135c commit 26028d9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">

<activity
android:name=".MainActivity"
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/tool/xfy9326/selectmedia/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MEDIA_SELECTOR_RESULT_CODE && data != null) {
if (data.getData() != null) {
if (outputMediaUri != null) {
saveMediaToExtraFile(data.getData());
saveMediaToExtraFile(data);
} else {
exitWithResult(data.getData());
exitWithResult(data);
}
} else {
Toast.makeText(this, R.string.no_select_media, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -104,8 +104,8 @@ private void launchMediaSelector() {
mediaSelectIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
} else {
mediaSelectIntent.setAction(Intent.ACTION_GET_CONTENT);
mediaSelectIntent.addCategory(Intent.CATEGORY_OPENABLE);
}
mediaSelectIntent.addCategory(Intent.CATEGORY_OPENABLE);
} else {
finish();
return;
Expand All @@ -125,22 +125,22 @@ private void launchMediaSelector() {
}
}

private void saveMediaToExtraFile(final Uri mediaUri) {
if (mediaUri != null && outputMediaUri != null) {
private void saveMediaToExtraFile(final Intent resultIntent) {
if (resultIntent.getData() != null && outputMediaUri != null) {
new TransferMediaAsync(getContentResolver(), isSuccess -> {
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();
}

Expand Down
25 changes: 11 additions & 14 deletions app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java
Original file line number Diff line number Diff line change
@@ -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<Uri, Void, Boolean> {
private static final String FILE_MODE_READ = "r";
Expand All @@ -23,25 +23,22 @@ class TransferMediaAsync extends AsyncTask<Uri, Void, Boolean> {
@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;
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<string name="app_name">选择媒体</string>
<string name="from_file_explorer">从文件管理器选择媒体</string>
<string name="from_file_album">从相册选择媒体</string>
<string name="loading">正在加载中……</string>
<string name="loading_msg">正在转移数据……\n路径:%1$s</string>
<string name="permission_grant_failed">权限授予失败!</string>
<string name="no_select_media">未选择任何媒体文件!</string>
<string name="media_file_transfer_error">媒体文件传输错误!</string>
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
<string name="app_name">Select Media</string>
<string name="from_file_explorer">Select media from file explorer</string>
<string name="from_file_album">Select media from album</string>
<string name="loading">Loading &#8230; &#8230;</string>
<string name="loading_msg">Transforming data now &#8230; &#8230;\nPath: %1$s</string>
<string name="permission_grant_failed">Permission grant failed!</string>
<string name="no_select_media">Select no media file!</string>
<string name="no_select_media">No media file selected!</string>
<string name="media_file_transfer_error">Media file transfer error!</string>
</resources>

Expand Down

0 comments on commit 26028d9

Please sign in to comment.