Skip to content

Commit bdf9db3

Browse files
committed
Fix FileUriExposedException on Android 7.0
1 parent 39cbb94 commit bdf9db3

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

example/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ android {
2121

2222
dependencies {
2323
compile project(':lib')
24+
compile 'com.android.support:support-core-utils:24.2.0'
2425
}

example/src/main/AndroidManifest.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="net.ypresto.androidtranscoder.example" >
3+
package="net.ypresto.androidtranscoder.example">
4+
5+
<uses-permission
6+
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
7+
android:maxSdkVersion="18" />
48

59
<application
610
android:allowBackup="true"
711
android:icon="@drawable/ic_launcher"
812
android:label="@string/app_name"
9-
android:theme="@style/AppTheme" >
13+
android:theme="@style/AppTheme">
1014
<activity
1115
android:name=".TranscoderActivity"
12-
android:label="@string/app_name" >
16+
android:label="@string/app_name">
1317
<intent-filter>
1418
<action android:name="android.intent.action.MAIN" />
1519

1620
<category android:name="android.intent.category.LAUNCHER" />
1721
</intent-filter>
1822
</activity>
23+
24+
<provider
25+
android:name="android.support.v4.content.FileProvider"
26+
android:authorities="net.ypresto.androidtranscoder.example.fileprovider"
27+
android:exported="false"
28+
android:grantUriPermissions="true">
29+
<meta-data
30+
android:name="android.support.FILE_PROVIDER_PATHS"
31+
android:resource="@xml/file_paths" />
32+
</provider>
1933
</application>
2034

2135
</manifest>

example/src/main/java/net/ypresto/androidtranscoder/example/TranscoderActivity.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.Bundle;
88
import android.os.ParcelFileDescriptor;
99
import android.os.SystemClock;
10+
import android.support.v4.content.FileProvider;
1011
import android.util.Log;
1112
import android.view.Menu;
1213
import android.view.MenuItem;
@@ -26,6 +27,7 @@
2627

2728
public class TranscoderActivity extends Activity {
2829
private static final String TAG = "TranscoderActivity";
30+
private static final String FILE_PROVIDER_AUTHORITY = "net.ypresto.androidtranscoder.example.fileprovider";
2931
private static final int REQUEST_CODE_PICK = 1;
3032
private static final int PROGRESS_BAR_MAX = 1000;
3133
private Future<Void> mFuture;
@@ -55,7 +57,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
5557
final File file;
5658
if (resultCode == RESULT_OK) {
5759
try {
58-
file = File.createTempFile("transcode_test", ".mp4", getExternalFilesDir(null));
60+
File outputDir = new File(getExternalFilesDir(null), "outputs");
61+
//noinspection ResultOfMethodCallIgnored
62+
outputDir.mkdir();
63+
file = File.createTempFile("transcode_test", ".mp4", outputDir);
5964
} catch (IOException e) {
6065
Log.e(TAG, "Failed to create temporary file.", e);
6166
Toast.makeText(this, "Failed to create temporary file.", Toast.LENGTH_LONG).show();
@@ -89,7 +94,10 @@ public void onTranscodeProgress(double progress) {
8994
public void onTranscodeCompleted() {
9095
Log.d(TAG, "transcoding took " + (SystemClock.uptimeMillis() - startTime) + "ms");
9196
onTranscodeFinished(true, "transcoded file placed on " + file, parcelFileDescriptor);
92-
startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file), "video/mp4"));
97+
Uri uri = FileProvider.getUriForFile(TranscoderActivity.this, FILE_PROVIDER_AUTHORITY, file);
98+
startActivity(new Intent(Intent.ACTION_VIEW)
99+
.setDataAndType(uri, "video/mp4")
100+
.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION));
93101
}
94102

95103
@Override
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths>
3+
<external-path
4+
name="output_videos"
5+
path="Android/data/net.ypresto.androidtranscoder.example/files/outputs" />
6+
</paths>

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

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)