将请求排入DownloadManager时,应用程序崩溃

时间:2017-06-13 11:19:55

标签: android android-download-manager

我试图制作一个使用DownloadManager下载iCal文件的应用。经过一番研究,我无法找到解决问题的方法。使用Android Studio调试器,我发现应用程序在请求时崩溃。这是我在Android Monitor中遇到的错误:

 java.lang.SecurityException: Permission Denial: writing com.android.providers.downloads.DownloadProvider uri content://downloads/my_downloads from pid=5082, uid=10208 requires android.permission.INTERNET, or grantUriPermission()

我不明白为什么它需要已经提供的android.permission.INTERNET,如下面的AndroidManifest所示:

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

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

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

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


</application>

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

     EditText url;
     Button buttonAdd;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         url = (EditText) findViewById(R.id.editTextLink);
         buttonAdd = (Button) findViewById(R.id.buttonADD);

         buttonAdd.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if(!url.getText().toString().isEmpty()) {
                     Uri iCalURI = Uri.parse(url.getText().toString());

                     DownloadData(iCalURI);

                 }
             }
         });


     }

     private void DownloadData(Uri p_uri) {

         DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
         DownloadManager.Request request = new DownloadManager.Request(p_uri);

         request.setDescription("iCal Calendar downloading");
         request.setTitle("iCal calendar");
         request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);                      //Restrict the types of networks over which this download may proceed.
         request.setAllowedOverRoaming(false);                                                                                               //Set whether this download may proceed over a roaming connection.
         request.allowScanningByMediaScanner();
         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "iCal_tmp.ics");

         manager.enqueue(request);           //this is where it crashes

         return;
     }
  }

我的权限或请求本身存在问题吗?

1 个答案:

答案 0 :(得分:2)

您需要的第一个授予权限。

public boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG, "Permission is granted");

            writeFilesToSdCard();
            return true;
        } else {

            Log.v(TAG, "Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else { //permission is automatically granted on sdk<23 upon installation
        writeFilesToSdCard();
        return true;
    }
}

然后用覆盖方法请求它。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG, "Permission: " + permissions[0] + "was " + grantResults[0]);
        //resume tasks needing this permission
    }
}

注意:不要忘记在<application>标记之外的清单中放置权限。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myproject">

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

    <application
        //
    </application>

</manifest>