当WRITE_EXTERNAL_STORAG​​E权限在运行时授予应用程序无法访问SD卡 [英] Application not able to access SD card when WRITE_EXTERNAL_STORAGE permission is granted at run time

查看:487
本文介绍了当WRITE_EXTERNAL_STORAG​​E权限在运行时授予应用程序无法访问SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

版本的Andr​​oid M 以访问SD卡具有强制停止,手动启动应用程序时,允许在运行时,如何通过编程实现它被授予?

不过SD卡可以访问该应用是否强制停止并重新启动。

AndroidManifest.xml中:

 使用许可权-SDK-M机器人:名字=android.permission.WRITE_EXTERNAL_STORAG​​E
     / * **检查许可**
        如果没有要求
         其他
         浏览文件目录
    * /    的String [] =烫发{Manifest.permission.WRITE_EXTERNAL_STORAG​​E};
    如果(checkSelfPermission(烫发[0])== PackageManager.PERMISSION_DENIED){
                    requestPermissions(烫发,REQ);                }其他{                    探索(); //方法来浏览文件目录
                }

onRequestPermissionsResult

  @覆盖
公共无效onRequestPermissionsResult(INT申请code,
        的String []的权限,INT [] grantResults){
    // TODO自动生成方法存根    如果(grantResults [0] == PackageManager.PERMISSION_GRANTED){   //无法浏览SD卡,直到重新启动应用程序
       探索(); //方法来浏览文件目录
    }其他{
        Log.i(onRequestPermissionsResult,请求被拒绝);
    }    super.onRequestPermissionsResult(要求code,权限,grantResults);
}


解决方案

您将需要重新启动应用程序,以获得许可WRITE_EXTERNAL_STORAG​​E(一种其他权限)。这是因为该许可实际上是Linux权限。 Android的最新preVIEW版本不会重新启动在这种情况下的应用,但也许谷歌将稍后补充。

您可以使用此code做重新启动:

  @覆盖
公共无效onRequestPermissionsResult(INT申请code,的String []的权限,INT [] grantResults){
    //写外部存储权限需要重新启动
    的for(int i = 0; I< permissions.length;我++)
        如果(Manifest.permission.WRITE_EXTERNAL_STORAG​​E.equals(许可[I])及和放大器;
                grantResults [I] == PackageManager.PERMISSION_GRANTED){
            Log.i(TAG,重新启动应用程序);            // 1秒后开始时间表
            PI的PendingIntent = PendingIntent.getActivity(
                    本,
                    0,
                    getIntent(),
                    PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC,System.currentTimeMillis的()+ 1000,PI);            //现在停止
            System.exit(0);
        }
}

编辑:你可以找到它们与这里的Linux权限相关联的Andr​​oid权限的列表:
https://android.googlesource.com/platform/frameworks/base/+/master/data/etc/platform.xml

In android M to access sdcard has to force stop and start app manually when permission is granted at runtime, how to achieve it programmatically?

However sdcard can be accessed if the app is force stop and restarted.

AndroidManifest.xml :

uses-permission-sdk-m android:name="android.permission.WRITE_EXTERNAL_STORAGE


     /* **Checking permission **
        if doesn't have request
         else
         browse file directory       
    */

    String[] perm = { Manifest.permission.WRITE_EXTERNAL_STORAGE };
    if (checkSelfPermission(perm[0]) == PackageManager.PERMISSION_DENIED) {


                    requestPermissions(perm, REQ);

                } else {

                    Explore(); //method to browse file directory
                }

onRequestPermissionsResult

   @Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    // TODO Auto-generated method stub

    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  

   //Not able to browse SD card until restart application


       Explore(); //method to browse file directory


    } else {
        Log.i("onRequestPermissionsResult", " Request denied");
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

解决方案

You will need to restart the application to obtain the WRITE_EXTERNAL_STORAGE permission (an some other permissions). This is because this permission is actually a Linux permission. The latest preview version of Android does not restart the application in this case, but maybe Google will add this later.

You could use this code to do the restart:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    // Write external store permission requires a restart
    for (int i = 0; i < permissions.length; i++)
        if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[i]) &&
                grantResults[i] == PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "Restarting application");

            // Schedule start after 1 second
            PendingIntent pi = PendingIntent.getActivity(
                    this,
                    0,
                    getIntent(),
                    PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pi);

            // Stop now
            System.exit(0);
        }
}

Edit: you can find a list of Android permissions which are associated with a Linux permission here: https://android.googlesource.com/platform/frameworks/base/+/master/data/etc/platform.xml

这篇关于当WRITE_EXTERNAL_STORAG​​E权限在运行时授予应用程序无法访问SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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