问题
how to save picture in instant app
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="***">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"/>
EasyPermissions.requestPermissions(this, "11", REQUEST_STORAGE_PERMISSION, Manifest.permission.WRITE_EXTERNAL_STORAGE);
java.io.FileNotFoundException: /storage/emulated/0/Android/data/***/files/pic.jpg (Permission denied)
回答1:
You Cannot access storage directly in Instant Apps. They run on a higher level security SandBox. The only permissions you have access are the following:
BILLING
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ACCESS_NETWORK_STATE
CAMERA
INSTANT_APP_FOREGROUND_SERVICE only in Android O.
INTERNET
READ_PHONE_NUMBERS only in Android O.
RECORD_AUDIO
VIBRATE
Any other permission will need the installed version of your app.
That means that you cannot read or write from the public storage. However you can access public content provider exposed to instant app, such as contact pickers or photopickers.
Source: Official FAQ here
回答2:
apply this code on your camera activies to get permissions
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (hasTakePermissions(getApplicationContext(), PERMISSIONS)) {
takePicture();
} else {
ActivityCompat.requestPermissions(HomeActivity.this, PERMISSIONS, PERMISSION_ALL);
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_WRITE_PERMISSION:
if (hasPermissions(getApplicationContext(), PERMISSIONS)) {
takePicture();
} else {
ActivityCompat.requestPermissions(HomeActivity.this, PERMISSIONS, PERMISSION_ALL);
}
}
}
public static boolean hasTakePermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
来源:https://stackoverflow.com/questions/44800338/how-to-save-picture-in-instant-app