0

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)

xixihaha
  • 23
  • 4

3 Answers3

3

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

gbaccetta
  • 4,449
  • 2
  • 20
  • 30
  • Beware that the content provider alternative might not be a valid approach. According to “Restricted features” and “Unsupported features”, https://developer.android.com/topic/instant-apps/prepare.html#additional-reqs, content providers are not supported. – Julia K Jul 22 '17 at 00:34
0

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;
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • camera permission has granted and camera can preview , but when take picture can't save it – xixihaha Jun 28 '17 at 11:16
  • for that case you need to verify the image saving path format –  Jun 28 '17 at 11:38
  • when i only request WRITE_EXTERNAL_STORAGE permission ,can't show the dialog .May be instant app can't use storage. – xixihaha Jun 28 '17 at 11:41
  • in instant app also u need to save the image in particular location .. can i see the ur total saving procedure –  Jun 28 '17 at 11:52
0

I'm late for the response, hopefully this will help someone

As mentioned, WRITE_EXTERNAL_STORAGE has Protection level: dangerous, therefore you can't use it. However, you can use Cache to save your images.

val outDirectory = File(activity?.cacheDir, "image.jpg")

Word of caution: Use it wisely, as running into an OOM would be inevitable if there are multiples images.

ishan_307
  • 21
  • 3