首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在MarshMallow的小部件中显示来自SDCard的图像

在MarshMallow的小部件中显示来自SDCard的图像
EN

Stack Overflow用户
提问于 2016-03-21 17:53:03
回答 4查看 398关注 0票数 16

在小部件中,我使用remoteView.setImageViewUri()显示来自SDCard的图像。此策略可以正常工作,但使用MarshMallow时除外:

错误是:

代码语言:javascript
复制
Unable to open content:  file:///storage/emulated/0/Android/data/applicaitonPackage/files/file.png
open failed: EACCESS (Permission denied)

很明显,这是一个权限问题,但我不知道如何为小部件容器提供权限,理论上(请参阅注释1)图像已经存储在共享存储中。

注意1:存储图像的目录是Environment.getExternalStorageDirectory()下的共享存储

注意2:应用程序不适用于MarshMallow,而是使用targetSdkVersion=15

注意3:不要只回复解释MarshMallow中新的运行时权限。我已经知道权限的变化,这不是问题,因为应用程序是针对SDKVersion 15的,并且应用程序访问外部存储没有任何问题,问题是小部件容器,我怀疑它没有权限。

EN

回答 4

Stack Overflow用户

发布于 2016-03-29 14:29:13

嗨,这里是android M的设置权限的几个步骤,记住你也应该在清单文件中声明相同的权限。

步骤1.声明全局变量:

代码语言:javascript
复制
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 0;

步骤2.在您的主活动中使用此代码

代码语言:javascript
复制
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
}
}

步骤3.在oncreate方法中调用此方法,

locationpermission();以及你可以从这里调用的任何权限,以及你可以在override method onRequestPermissionsResult中得到的每一个结果。

谢谢你

希望这能对你有所帮助(Y)。

请参考此处的示例

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

票数 3
EN

Stack Overflow用户

发布于 2016-04-04 22:24:02

@Docs说

在运行时请求权限

从Android 6.0 (API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时授予权限。这种方法简化了应用程序的安装过程,因为用户在安装或更新应用程序时不需要授予权限。它还让用户对应用程序的功能有了更多的控制;

因此,这是有意义的,尽管您在清单中声明了权限,但仍然得到了拒绝的权限

我建议您阅读如何获得运行时权限

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

以下是Docs提供的示例

代码语言:javascript
复制
    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.
}

}

根据您的要求更改权限

希望它能指引你找到正确的方向

票数 2
EN

Stack Overflow用户

发布于 2016-04-01 17:02:25

您的清单文件中有此权限吗?

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36127733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档