当前位置: 首页 > 知识库问答 >
问题:

如何在android M上请求访问图库的权限?

公孙英飙
2023-03-14

我有一个应用程序,将选择图像到画廊,并显示它的测试使用ImageView。我的问题是它不能在Android M上工作。我可以选择图像,但不能在我的测试中显示。他们说我需要得到允许才能在Android M上访问图像,但不知道怎么做。请帮帮忙。

共有1个答案

陆星文
2023-03-14

从Android6.0(API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时授予应用程序权限。

类型1-当你的应用程序请求权限时,系统会向用户呈现一个对话框。当用户响应时,系统调用应用程序的onRequestPermissionsResult()方法,将用户响应传递给它。您的应用程序必须重写该方法,以查明是否授予了权限。向回调传递的请求代码与您传递给requestPermissions()的请求代码相同。

private static final int PICK_FROM_GALLERY = 1;

ChoosePhoto.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick (View v){
    try {
        if (ActivityCompat.checkSelfPermission(EditProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(EditProfileActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
        } else {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
});


@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
    {
       switch (requestCode) {
            case PICK_FROM_GALLERY:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
                } else {
                    //do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
                }
                break;
        }
    }

类型2-如果您想在一个地方背靠背地授予运行时权限,那么您可以遵循下面的链接

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

注意-如果manifest.permission.read_external_storage产生错误,请将其替换为android.manifest.permission.read_external_storage。

==>如果您想了解更多有关运行时权限的信息,请访问下面的链接

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

 dependencies {
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'

// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
 @Override
     public void onRequestPermissionsResult(int requestCode, String[] 
    permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions,grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

请求权限

private static final int LOCATION_REQUEST = 222;

调用此方法

@AfterPermissionGranted(LOCATION_REQUEST)
private void checkLocationRequest() {
   String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};
    if (EasyPermissions.hasPermissions(this, perms)) {
        // Already have permission, do the thing
        // ...
    } else {
        // Do not have permissions, request them now
        EasyPermissions.requestPermissions(this,"Please grant permission",
                LOCATION_REQUEST, perms);
    }
}

可选地,对于更精细的控件,您可以让您的活动/片段实现PermissionCallbacks接口。

 @Override
public void onPermissionsGranted(int requestCode, List<String> list) {
    // Some permissions have been granted
    // ...
}

@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
    // Some permissions have been denied
    // ...
}
implementation 'com.github.florent37:runtime-permission-kotlin:1.1.2'
        askPermission(
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    ) {
       // camera or gallery or TODO
    }.onDeclined { e ->
        if (e.hasDenied()) {
            AlertDialog.Builder(this)
                .setMessage(getString(R.string.grant_permission))
                .setPositiveButton(getString(R.string.yes)) { dialog, which ->
                    e.askAgain()
                } //ask again
                .setNegativeButton(getString(R.string.no)) { dialog, which ->
                    dialog.dismiss()
                }
                .show()
        }

        if (e.hasForeverDenied()) {
            e.goToSettings()
        }
    }

链接->https://github.com/florent37/runtimepermission

 类似资料:
  • 我想使用 null 我想创建自定义组权限,只问我一个请求,只给我一个响应。 谢谢

  • 听说Android Q推出了一个名为“范围存储”的新安全功能,限制外部存储中的访问文件。我的问题是我必须从应用程序中保存一个文本文档到用户指定的位置。这是否需要任何类型的权限,而不是Q设备中的和?

  • 我正试图让我的应用程序为新的Android M权限更改做好准备,发现了一些奇怪的行为。我的应用程序使用相机意图机制,允许用户从相机中获得一张照片。但是在另一个活动中,需要使用具有camera权限的camera本身(因为库依赖项card.io需要这样做)。 这是我用来让用户用相机点击一张照片和/或选择一张图像的代码 我在活动中单击按钮时调用。当我没有相机权限在我的应用程序,它的工作很好,但与添加,我

  • 我正在编写一个处理多个系统的应用程序。用户可以选择他想使用的系统,我将该系统ID存储在会话(客户端会话)中 现在我有了服务类,比如说CustomerService。 我想使用 Guice 将客户实例注入控制器。但是我想使用存储在会话中的系统ID实例化客户服务。 如何访问<code>请求。Guice模块中的会话? 编辑: 简化了我上面的代码。我的实际代码使用接口。我如何使用辅助注射? 这给了我:客户

  • 问题内容: 我想实现我的应用程序,是 不是 需要右键单击并选择 以管理员身份运行 我要运行它每次。我希望Windows像其他Windows应用程序一样提示我获得管理员权限。 考虑以下代码: 如果您编译它并双击它,它将打印: 打开:C:\ Windows \ test.txt:拒绝访问。 但是,如果右键单击并以管理员身份运行,它将创建并写入文件。 如何使其仅通过双击即可要求管理员权限? 问题答案:

  • 嗨,我正在使用SoapUI Pro来测试一组Soap网络服务。 我有一个 testRunListener,它将我的 soap 请求的请求和响应记录到它在运行测试时创建的文件中。在这里,我有一个if语句,该语句在记录请求和响应之前检查测试步骤的名称.. 而不是使用单个请求的名称,我想使用更通用的东西,例如请求的类型,可以是 createShipping 或 cancelShipping。这是因为我有