16

In a widget I display images from SDCard using remoteView.setImageViewUri(). This strategy works correctly except with MarshMallow:

The error is:

Unable to open content:  file:///storage/emulated/0/Android/data/applicaitonPackage/files/file.png
open failed: EACCESS (Permission denied)

It's clear that this is a permission problem, but I don't know how to give permissions to the widget container and in theory (see Note 1) the images are already stored in shared storage.

Note 1: The directory where images are stored is shared storage under Environment.getExternalStorageDirectory()

Note 2: Application is not adapted to MarshMallow and uses targetSdkVersion=15

Note 3: Don't just reply explaining about new runtime permissions in MarshMallow. I already know permissions changes and that is not the problem because application is targeted SDKVersion 15 and the app hasn't any problem accessing the external storage, the problem is with the widget container that is the one that I suspect that doesn't has the permissions.

lujop
  • 13,504
  • 9
  • 62
  • 95

3 Answers3

2

@Docs says

Requesting Permissions at Run Time

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality;

Therefore this make sense although you declare permission in manifest but still getting permission denied

I suggest you to read how to get runtime permission

http://developer.android.com/training/permissions/requesting.html

Here is the example provided by Docs

    if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an expanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}

}

Change permission as per you requirement

Hope it leads you to right direction

Rachit Solanki
  • 369
  • 6
  • 13
0

Did you have this permisstion in your Manifest file?

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
  • 1
    I have the WRITE_EXTERNAL_STORAGE that according docs grants the READ one. But the problem is not in the app, the problem is in the WidgetContainer that is ran in another process by OS. – lujop Apr 04 '16 at 13:16
0

If I set the targetSdkVersion to 15 instead of 23 - will the android M users see the app and be able to download it (granting all permissions at runtime)?

Yes, the app will be available to M users and every permission is granted at install time.

`targetSdkVersion=15` 

which is less than 23.User will able to download the app and use it it grants all the permission at run time. If you want to check the permission go to settings and grant the permission. If you want to use the android M permission module at run time you have to set the target SDK version to 23. If set the targetSdkVersion=15 on android M and higher devices app getting crashed.

If you want to support android M users set targetSdkVersion=23and handle permission runtime.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59