下载文件并在android

时间:2016-04-12 15:57:49

标签: java android

在我的Android应用程序中。当有人试图下载文件时,它应该要求存储权限..如果被授予,它应该开始下载文件。 下次如果已经授予权限,则只需启动新下载。 这是我的代码的一部分...

        public void downloadFile(String url) {
            Uri source = Uri.parse(url);

            // Make a new request pointing to the download url
            DownloadManager.Request request = new DownloadManager.Request(source);
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

            String fileExtension = MimeTypeMap.getFileExtensionFromUrl(url);
            String fileName = URLUtil.guessFileName(url, null, fileExtension);

            // save the file in the "Downloads" folder of SDCARD
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
            // get download service and enqueue file
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.contains("?dl=1")) {
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {
                    downloadFile(url);
                } else {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_WRITE_EXTERNAL_STORAGE);

                }
                return true;

            }

此代码的问题是:

当用户拒绝权限应用崩溃时。

当用户允许第一次授权时,下载不会立即开始...用户需要再次点击下载链接。

2 个答案:

答案 0 :(得分:0)

您的shouldOverrideUrlLoading方法始终返回true。这是想要的行为吗?

尝试添加权限请求响应代码,如下所示:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], 
                                          int[] grantResults) {
    switch (requestCode) {
        case REQUEST_WRITE_EXTERNAL_STORAGE: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // do your work
            } else {
                // permission denied
            }
            return;
        }
    }
}

来源:http://developer.android.com/training/permissions/requesting.html#perm-request

答案 1 :(得分:0)

您需要实现回调函数ActivityCompat.OnRequestPermissionsResultCallback

所以第一步是将类签名更改为类似的内容,但要小心扩展您需要权限结果的活动,MainActivity只是一个示例:

public class MainActivity extends AppCompatActivity
        implements ActivityCompat.OnRequestPermissionsResultCallback

第二步是覆盖以下功能:

public void onRequestPermissionsResult(int requestCode, String[] permissions,
            int[] grantResults) {

   if( requestCode == REQUEST_WRITE_EXTERNAL_STORAGE)
   {
       if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
             //Do something if the permission is granted
       }
       else
       {
             //Do something if the permission denied
       }
   }
}