search

Home  >  Q&A  >  body text

关于Android的写权限

1. 问题描述

昨晚在更新了公司的app,接着推送出去了,发现有人反馈说自动更新用不了。我调试了一下,定位到代码和报错信息如下:
代码:

               try {
                    //创建下载任务,downloadUrl就是下载链接
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("xxxx下载链接"))
                            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS, "Honey_.apk")
                            .setTitle("下载标题")
                            .setMimeType("")
                            .setVisibleInDownloadsUi(true)
                            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                            .setDescription("下载的具体描述");
                    //获取下载管理器
                    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    //将下载任务加入下载队列,否则不会进行下载
                    long TaskID = downloadManager.enqueue(request);
                    LogUtils.e(OpenLog,TAG,"TaskId:"+TaskID);
                } catch (Exception e) {
                    LogUtils.e(OpenLog,TAG,"Exception2:"+e.getMessage());
                }

long TaskID = downloadManager.enqueue(request);这里,抛出异常了:

E/MainActivity: Exception2:need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10207 does not have android.permission.WRITE_EXTERNAL_STORAGE.

2. 解决答案

报错信息是,我没有声明权限?查了一下,我是有声明的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="testmodules">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name=".chestnut.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/app_ico"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".chestnut.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".chestnut.Main2Activity" />

        <activity android:name=".chestnut.DialogActivity"
            android:theme="@style/DialogTransparent" />

    </application>

</manifest>

之前一直是这样声明的,也没有报错什么的,
然后,Google,百度一下,发现,要写成如下的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="testmodules">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:name=".chestnut.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/app_ico"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".chestnut.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

额,Application开始前,Manifest开始后,要加入写权限,Application结束后,Manifest结束前,要加入写权限,这样做,才,没问题...

那,是为什么要这样做...?

大家讲道理大家讲道理2772 days ago410

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 18:03:40

    This solution is too strange, I believe this is not the correct solution!

    Android 6.0 (API LEVEL 23) will need to dynamically apply for permissions in the future, and of course the permissions need to be declared in AndroidManifest.xml. Dynamic permission application requires the client to actively apply for permissions from the user in the form of a dialogue window before performing the operation. If If the application is successful, then proceed to perform subsequent operations; if the application fails (or does not apply at all), directly perform operations that require permission application, then wait for the error to be reported...

    There are many well-packaged libraries on github, you can learn about it yourself. Highly recommended:

    • googlesamples/easypermissions

    p.s. Other libraries will always have some problems, please see issues for details.

    reply
    0
  • Cancelreply