Skip to content

Commit 0527fb9

Browse files
authored
CAMERA: use FileProvider on Android to read and write data (#380)
Apps that use FileProvider to pass URIs to activities must include ANDROID_xml_services and xml/file_paths.xml. Also fixes build-binary to locate the support.v4 library needed for FileProvider. Finally, use onRequestPermissionsResult to bootstrap.java.in to fix permission requests hanging.
1 parent 9b3d9e3 commit 0527fb9

File tree

8 files changed

+100
-6
lines changed

8 files changed

+100
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<provider
2+
android:name="android.support.v4.content.FileProvider"
3+
android:authorities="@[email protected]"
4+
android:exported="false"
5+
android:grantUriPermissions="true">
6+
<!-- resource file to create -->
7+
<meta-data
8+
android:name="android.support.FILE_PROVIDER_PATHS"
9+
android:resource="@xml/file_paths">
10+
</meta-data>
11+
</provider>

apps/DemoCamera/xml/file_paths.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<paths>
3+
<root-path name="root" path="." />
4+
</paths>

loaders/android/bootstrap.java.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ public class @SYS_APPNAME@ extends Activity implements @ANDROID_JAVA_IMPLEMENTS@
106106
return true;
107107
}
108108

109+
@IF_ANDROIDAPI_GT_22@
110+
@Override
111+
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
112+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
113+
//Must do something with results data or app will hang
114+
int rc = requestCode;
115+
String p = permissions[0];
116+
int gr = grantResults[0];
117+
}
118+
/* end of IF_ANDROIDAPI_GT_22 */
119+
109120
@Override
110121
public void startActivityForResult(Intent intent, int cont) {
111122
try {

modules/camera/ANDROID_java_activityadditions

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ private void startCamera(String fnl_name, String tmp_name) {
1414
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
1515
File f = new File(camera_tmp);
1616
if (f != null) {
17-
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
17+
Uri imageUri = FileProvider.getUriForFile(this,
18+
19+
f);
20+
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
21+
if (@SYS_ANDROIDAPI@ <= 22) {
22+
intent.setClipData(ClipData.newRawUri("", imageUri));
23+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
24+
}
1825
// intent.putExtra(android.provider.MediaStore.EXTRA_SCREEN_ORIENTATION,ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
1926
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
2027
}
@@ -26,7 +33,14 @@ private void startVidCamera(String fnl_name, String tmp_name, int maxlength) {
2633
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
2734
File f = new File(vid_tmp);
2835
if (f != null) {
29-
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
36+
Uri vidUri = FileProvider.getUriForFile(this,
37+
38+
f);
39+
intent.putExtra(MediaStore.EXTRA_OUTPUT, vidUri);
40+
if (@SYS_ANDROIDAPI@ <= 22) {
41+
intent.setClipData(ClipData.newRawUri("", vidUri));
42+
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
43+
}
3044
if (maxlength > 0) {
3145
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, maxlength);
3246
}

modules/camera/ANDROID_java_imports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ import android.media.ExifInterface;
1010
import java.io.FileOutputStream;
1111
import java.io.IOException;
1212

13+
import android.support.v4.content.FileProvider;
14+
1315

modules/videoplayer/ANDROID_java_activityadditions

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
private void startVideoPlayer(String mov_name, int orientation) {
33
File f = new File(mov_name);
44
if (f != null) {
5-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(f));
6-
intent.setDataAndType(Uri.fromFile(f), "video/*");
5+
Uri vidUri = FileProvider.getUriForFile(this,
6+
7+
f);
8+
Intent intent = new Intent(Intent.ACTION_VIEW, vidUri);
9+
intent.setDataAndType(vidUri, "video/*");
10+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
711
if (orientation == 1) {
812
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
913
} else if (orientation == 2) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11

22
import java.io.File;
33

4+
import android.support.v4.content.FileProvider;
5+

targets/android/build-binary

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,54 @@ else
144144
assert "### Gradle is not supported"
145145
fi
146146

147+
# try to include support.v4 library
147148
if [ -f $ANDROIDSDK/extras/android/support/v4/android-support-v4.jar ]; then
148149
cp -r $ANDROIDSDK/extras/android/support/v4/android-support-v4.jar $tmpdir/libs
150+
echo " => support-v4 library loaded"
151+
else
152+
# look for support-v4 in alternate location
153+
# special support for API 19
154+
if [ $SYS_ANDROIDAPI -eq "19" ]; then
155+
if [ -f $ANDROIDSDK/extras/android/m2repository/com/android/support/support-v4/19.1.0/support-v4-19.1.0.jar ]; then
156+
cp -r $ANDROIDSDK/extras/android/m2repository/com/android/support/support-v4/19.1.0/support-v4-19.1.0.jar $tmpdir/libs
157+
echo " => support-v4 library loaded (version 19)"
158+
fi
159+
else
160+
# Find the directory containing the support-v4 AAR file
161+
# Only folders from 20 - 23 have the full AAR file needed
162+
supportv4version=$SYS_ANDROIDAPI
163+
if [ $supportv4version -gt 23 ]; then
164+
supportv4version="23"
165+
fi
166+
v4dir=""
167+
while [ $supportv4version -gt "19" ]; do
168+
v4dir=`find $ANDROIDSDK/extras/android/m2repository/com/android/support/support-v4 -name "$supportv4version*" | grep -v -e "alpha" -e "beta" | sort | tail -1`
169+
if [ ! "X$v4dir" = "X" ]; then
170+
break
171+
fi
172+
supportv4version=`expr ${supportv4version} - 1`
173+
done
174+
# If a directory is found, check if classes.jar has already been extracted. If not, extract it from the AAR
175+
if [ ! "X$v4dir" = "X" ]; then
176+
v4file=`find $v4dir -name classes.jar`
177+
if [ ! -s "$v4file" ]; then
178+
v4aar=`find $v4dir -name support-v4*.aar`
179+
if [ -s "$v4aar" ]; then
180+
unzip $v4aar classes.jar -d $v4dir > /dev/null
181+
v4file=`find $v4dir -name classes.jar`
182+
fi
183+
fi
184+
# If a classes.jar file has been found, copy it to libs
185+
if [ -s "$v4file" ]; then
186+
cp -r $v4file $tmpdir/libs
187+
echo " => support-v4 library loaded (version $supportv4version)"
188+
else
189+
echo " => warning: support-v4 library not found"
190+
fi
191+
else
192+
echo " => warning: support-v4 library not found"
193+
fi
194+
fi
149195
fi
150196

151197
if [ "$NEED_GCM" = "yes" ]; then
@@ -185,7 +231,7 @@ if [ -d "$jarfilesdir" ]; then
185231
mkdir -p $tmpdir/libs/
186232
for jar in $jarfiles; do
187233
locajar=`basename $jar | tr A-Z a-z`
188-
vecho " => coping jar file - $locajar ..."
234+
vecho " => copying jar file - $locajar ..."
189235
cp $jar $tmpdir/libs/
190236
done
191237
fi
@@ -197,7 +243,7 @@ if [ -d "$xmlfilesdir" ]; then
197243
mkdir -p $tmpdir/res/xml/
198244
xmlfiles=`ls -1 $xmlfilesdir/*.xml 2> /dev/null`
199245
for xml in $xmlfiles; do
200-
vecho " => coping xml file - $xml ..."
246+
vecho " => copying xml file - $xml ..."
201247
cp $xml $tmpdir/res/xml/
202248
done
203249
fi

0 commit comments

Comments
 (0)