如何获得访问android M.上的图库的权限? [英] How to ask permission to access gallery on android M.?

查看:87
本文介绍了如何获得访问android M.上的图库的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,它将选择图像到图库并使用Imageview将其显示给测试。我的问题是它不适用于Android M.我可以选择图像,但不会显示在我的测试。他们说我需要获得访问Android M上的图像的权限,但不知道如何。请帮助。

I have this app that will pick image to gallery and display it to the test using Imageview. My problem is it won't work on Android M. I can pick image but won't show on my test.They say i need to ask permission to access images on android M but don't know how. please help.

推荐答案

从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是当他们安装应用程序时。

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

类型1 - 当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的onRequestPermissionsResult()方法,并向其传递用户响应。您的应用必须覆盖该方法,以确定是否已授予权限。回调传递给您传递给requestPermissions()的相同请求代码。

Type 1- When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to 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 - 如果你想在一个地方背靠背给予运行时权限然后你可以按照以下链接

Type 2- If you want to give runtime permission back to back in one place then you can follow below link

Android 6.0多个权限

并在Manifest中添加权限以满足您的要求

And in Manifest add permission for your requirements

<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。

Note- If Manifest.permission.READ_EXTERNAL_STORAGE produce error then please replace this with android.Manifest.permission.READ_EXTERNAL_STORAGE.

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

==> If you want to know more about runtime permission then please follow below link

https://developer.android。 com / training / permissions / reques.html

这篇关于如何获得访问android M.上的图库的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆