Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying images from SDCard in a Widget from MarshMallow

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.

like image 416
lujop Avatar asked Mar 21 '16 09:03

lujop


People also ask

How to use external SD card as internal storage on Marshmallow?

Once you format your external SD card as internal storage on Marshmallow, it can only be used on the same device then. To use it on other devices, a format will be required, which ensures the safety of your data on the SD card.

How to view thumbnails from the SD card on Android?

UPDATE: In order to be bale to view thumbnails images from the SD Card, Android needs to create them first, hence you should start the Gallery application that comes preinstalled, and open the sdcard folder which will automatically create thumbnails for the images stored on your sdcard.

How to display folder images on SD card using mediastore?

Give Image url as your sd card image path. it is not possible to display particular folder images using the mediastore method. because every images stored in the mediastore has different unique id to identify it.

How do I put a 24-bit image on an SD card?

So you can chose whatever suits you, either change the screen rotation or the image rotation on your PC… then remeber or copy the image name and that’s it, plug your SD card in the shield. Those are the name of my bmp 24 bits images on SD card for like “Img1” we call it in the code by using “Img1.bmp”.


2 Answers

hi here is few steps for setup permission for android M and remember you should declare same permission in manifest file as well.

Step 1. Declare global variable :

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 0;

Step 2. Use this code in your main activity

private void locationpermission() {
// Here, thisActivity is the current activity
 if (ContextCompat.checkSelfPermission(activity
    ,
    Manifest.permission.ACCESS_COARSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {

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

    // 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(activity,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            MY_PERMISSIONS_REQUEST_LOCATION);

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

  @Override
  public void onRequestPermissionsResult(int requestCode,
                               String permissions[], int[] grantResults)         {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // permission was granted, yay! Do the
        // contacts-related task you need to do.

    } else {

        // permission denied, boo! Disable the
        // functionality that depends on this permission.
    }
    return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}

Step 3. Call this method in your oncreate method,

locationpermission(); And anyone permission you can call from here and every result you can get in override method onRequestPermissionsResult this one.

thankyou

hope this will help you (Y).

please refer from here example

https://www.dropbox.com/s/w29sljy0zpwwm61/MyApplication.zip?dl=0

like image 87
Ashim Kansal Avatar answered Oct 04 '22 12:10

Ashim Kansal


@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

like image 2
Rachit Solanki Avatar answered Oct 04 '22 10:10

Rachit Solanki