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

Commit 26028d9

Browse files
committed
Fix: support old app in sandbox storage on android Q
Fix: uri in return intent may not have read permission Fix: other problems
1 parent c1d135c commit 26028d9

File tree

6 files changed

+25
-31
lines changed

6 files changed

+25
-31
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "tool.xfy9326.selectmedia"
99
minSdkVersion 14
1010
targetSdkVersion 29
11-
versionCode 5
12-
versionName "1.2.2"
11+
versionCode 6
12+
versionName "1.2.3"
1313

1414
buildTypes {
1515
release {

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<application
1010
android:icon="@mipmap/ic_launcher"
1111
android:label="@string/app_name"
12+
android:requestLegacyExternalStorage="true"
1213
android:roundIcon="@mipmap/ic_launcher"
13-
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
14+
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">
1415

1516
<activity
1617
android:name=".MainActivity"

app/src/main/java/tool/xfy9326/selectmedia/MainActivity.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
4040
if (requestCode == MEDIA_SELECTOR_RESULT_CODE && data != null) {
4141
if (data.getData() != null) {
4242
if (outputMediaUri != null) {
43-
saveMediaToExtraFile(data.getData());
43+
saveMediaToExtraFile(data);
4444
} else {
45-
exitWithResult(data.getData());
45+
exitWithResult(data);
4646
}
4747
} else {
4848
Toast.makeText(this, R.string.no_select_media, Toast.LENGTH_SHORT).show();
@@ -104,8 +104,8 @@ private void launchMediaSelector() {
104104
mediaSelectIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
105105
} else {
106106
mediaSelectIntent.setAction(Intent.ACTION_GET_CONTENT);
107-
mediaSelectIntent.addCategory(Intent.CATEGORY_OPENABLE);
108107
}
108+
mediaSelectIntent.addCategory(Intent.CATEGORY_OPENABLE);
109109
} else {
110110
finish();
111111
return;
@@ -125,22 +125,22 @@ private void launchMediaSelector() {
125125
}
126126
}
127127

128-
private void saveMediaToExtraFile(final Uri mediaUri) {
129-
if (mediaUri != null && outputMediaUri != null) {
128+
private void saveMediaToExtraFile(final Intent resultIntent) {
129+
if (resultIntent.getData() != null && outputMediaUri != null) {
130130
new TransferMediaAsync(getContentResolver(), isSuccess -> {
131131
if (isSuccess) {
132-
exitWithResult(mediaUri);
132+
exitWithResult(resultIntent);
133133
} else {
134134
Toast.makeText(MainActivity.this, R.string.media_file_transfer_error, Toast.LENGTH_SHORT).show();
135135
onBackPressed();
136136
}
137137

138-
}).execute(mediaUri, outputMediaUri);
138+
}).execute(resultIntent.getData(), outputMediaUri);
139139
}
140140
}
141141

142-
private void exitWithResult(Uri mediaUri) {
143-
setResult(RESULT_OK, new Intent().setData(mediaUri));
142+
private void exitWithResult(Intent resultIntent) {
143+
setResult(RESULT_OK, resultIntent);
144144
finish();
145145
}
146146

app/src/main/java/tool/xfy9326/selectmedia/TransferMediaAsync.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package tool.xfy9326.selectmedia;
22

33
import android.content.ContentResolver;
4-
import android.content.res.AssetFileDescriptor;
54
import android.net.Uri;
65
import android.os.AsyncTask;
76

87
import androidx.annotation.NonNull;
98

109
import java.nio.channels.FileChannel;
10+
import java.util.Objects;
1111

1212
class TransferMediaAsync extends AsyncTask<Uri, Void, Boolean> {
1313
private static final String FILE_MODE_READ = "r";
@@ -23,25 +23,22 @@ class TransferMediaAsync extends AsyncTask<Uri, Void, Boolean> {
2323
@Override
2424
protected Boolean doInBackground(Uri... uris) {
2525
if (uris.length >= 2) {
26-
try (AssetFileDescriptor inputFileDescriptor = resolver.openAssetFileDescriptor(uris[0], FILE_MODE_READ);
27-
AssetFileDescriptor outputFileDescriptor = resolver.openAssetFileDescriptor(uris[1], FILE_MODE_WRITE)) {
28-
if (inputFileDescriptor != null && outputFileDescriptor != null) {
29-
try (FileChannel inputChannel = inputFileDescriptor.createInputStream().getChannel();
30-
FileChannel outputChannel = outputFileDescriptor.createOutputStream().getChannel()) {
31-
long size = inputChannel.size();
32-
while (size > 0) {
33-
long count = outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
34-
if (count > 0) {
35-
size -= count;
36-
}
37-
}
26+
Uri mediaUri = uris[0];
27+
Uri mediaOutputUri = uris[1];
28+
try (FileChannel inputChannel = Objects.requireNonNull(resolver.openAssetFileDescriptor(mediaUri, FILE_MODE_READ)).createInputStream().getChannel();
29+
FileChannel outputChannel = Objects.requireNonNull(resolver.openAssetFileDescriptor(mediaOutputUri, FILE_MODE_WRITE)).createOutputStream().getChannel()) {
30+
long size = inputChannel.size();
31+
while (size > 0) {
32+
long count = outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
33+
if (count > 0) {
34+
size -= count;
3835
}
3936
}
37+
return true;
4038
} catch (Exception e) {
4139
e.printStackTrace();
4240
return false;
4341
}
44-
return true;
4542
}
4643
return false;
4744
}

app/src/main/res/values-zh/strings.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<string name="app_name">选择媒体</string>
44
<string name="from_file_explorer">从文件管理器选择媒体</string>
55
<string name="from_file_album">从相册选择媒体</string>
6-
<string name="loading">正在加载中……</string>
7-
<string name="loading_msg">正在转移数据……\n路径:%1$s</string>
86
<string name="permission_grant_failed">权限授予失败!</string>
97
<string name="no_select_media">未选择任何媒体文件!</string>
108
<string name="media_file_transfer_error">媒体文件传输错误!</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
<string name="app_name">Select Media</string>
44
<string name="from_file_explorer">Select media from file explorer</string>
55
<string name="from_file_album">Select media from album</string>
6-
<string name="loading">Loading &#8230; &#8230;</string>
7-
<string name="loading_msg">Transforming data now &#8230; &#8230;\nPath: %1$s</string>
86
<string name="permission_grant_failed">Permission grant failed!</string>
9-
<string name="no_select_media">Select no media file!</string>
7+
<string name="no_select_media">No media file selected!</string>
108
<string name="media_file_transfer_error">Media file transfer error!</string>
119
</resources>
1210

0 commit comments

Comments
 (0)