Skip to content

Commit

Permalink
Use FileProvider to open image/video in MediaLayout (getodk#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 authored and lognaturel committed Oct 9, 2018
1 parent 2a05303 commit 9a227b6
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.AppCompatImageButton;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
Expand All @@ -36,6 +37,7 @@
import org.javarosa.core.model.FormIndex;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
import org.odk.collect.android.BuildConfig;
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.listeners.AudioPlayListener;
Expand Down Expand Up @@ -172,7 +174,10 @@ public void playVideo() {
}

Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.fromFile(videoFile), "video/*");
Uri uri =
FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", videoFile);
FileUtils.grantFileReadPermissions(intent, uri, getContext());
intent.setDataAndType(uri, "video/*");
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(intent);
} else {
Expand Down Expand Up @@ -275,7 +280,10 @@ private void openImage() {
try {
File bigImage = new File(referenceManager.DeriveReference(bigImageURI).getLocalURI());
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.fromFile(bigImage), "image/*");
Uri uri =
FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", bigImage);
FileUtils.grantFileReadPermissions(intent, uri, getContext());
intent.setDataAndType(uri, "image/*");
getContext().startActivity(intent);
} catch (InvalidReferenceException e) {
Timber.e(e, "Invalid image reference due to %s ", e.getMessage());
Expand Down

0 comments on commit 9a227b6

Please sign in to comment.