0
你好我是Android Studio新手,我正在处理我的webView中的DownloadListener,以便下载已经上传到网页上的文件,基本上你可以说一个文件在pdf格式,但我想我的水平最好找错误,但未能发现错误one.The在于我们点击网页上的一些下载链接此刻却显示错误,是这样说:在Android中处理下载监听器
02-05 22:47:29.425 3531-3531/myapp.hp.com.abhivyakti E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp.hp.com.abhivyakti, PID: 3531
java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/download: Neither user 10059 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
at android.content.ContentResolver.insert(ContentResolver.java:1231)
at android.app.DownloadManager.enqueue(DownloadManager.java:946)
at myapp.hp.com.abhivyakti.Notification.NotifyFragment$3.onDownloadStart(NotifyFragment.java:95)
at com.android.webview.chromium.WebViewContentsClientAdapter.onDownloadStart(WebViewContentsClientAdapter.java:884)
at com.android.org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:112)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的活动代码如下:
public class NotifyFragment extends Fragment {
public NotifyFragment() {
// Required empty public constructor
}
private WebView webView;
private ProgressBar pb;
private ConnectivityManager cManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_notify, container, false);
webView = (WebView)view.findViewById(R.id.notifywebView);
pb = (ProgressBar)view.findViewById(R.id.notifyprogressBar);
cManager = (ConnectivityManager) getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if (nInfo == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("No Internet Connection")
.setTitle("Oops!!")
.setIcon(R.drawable.caution);
builder.setCancelable(false);
builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
webView.loadUrl("https://docs.google.com/document/d/12u6jzoY8zB11valB2FwMyrf0HcWDnOvCkokzggNu1bA/edit?usp=sharing");
}
});
AlertDialog alert = builder.create();
alert.show();
}else {
webView.loadUrl("https://docs.google.com/document/d/12u6jzoY8zB11valB2FwMyrf0HcWDnOvCkokzggNu1bA/edit?usp=sharing");
}
//getActivity().getWindow().requestFeature(Window.FEATURE_PROGRESS);
getActivity().getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//Make the bar disappear after URL is loaded, and changes string to Loading...
pb.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if (progress == 100) {
pb.setVisibility(View.GONE);
} else {
pb.setVisibility(View.VISIBLE);
}
}
});
webView.setWebViewClient(new mywebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/document/d/12u6jzoY8zB11valB2FwMyrf0HcWDnOvCkokzggNu1bA/edit?usp=sharing");
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getActivity().getSystemService(getActivity().DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getActivity(), "Downloading File", Toast.LENGTH_SHORT).show();
}
});
return view;
}
public class mywebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return onKeyDown(keyCode, event);
}
}
I w不好意思,因为它会帮助我向前发展并给予免费的应用程序。谢谢。
谢谢您的考虑。正如我所做的那样,增加了你提到的相同权限,但它给了我同样的错误。我已将最低使用的sdk分配给Jellybean,即4.0。 –