public class MainActivity extends AppCompatActivity {
private WebView webView;
String url = "https://svidzdownloader.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String s, String s1, String s2, String s3,
long l) {
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)getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
}
我制作了一个webview应用程序,我想从我的webview应用程序下载视频,并且已经在上面添加了下载功能代码,但是当我按下下载按钮导致我的应用程序崩溃时,该代码不起作用
答案 0 :(得分:0)
没有写权限到
/storage/emulated/0/Download/Download
: 用户10143或当前进程都没有android.permission.WRITE_EXTERNAL_STORAGE
转到AndroidManifest.xml
标签的<application
及以上:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
似乎您也需要运行时权限。例如,在您的onDownloadStart
中添加以下代码:
@Override
public void onDownloadStart(String s, String s1, String s2, String s3,
long l) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// permission has not been granted.
// Request for the permission like adding a function which will do that
requestPermission(); // a function to request permissions
} else {
// Permission granted!! do your stuff here
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)getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
请求许可的功能:
private void requestPermission() {
Log.i(TAG, "permission has NOT been granted. Requesting permission.");
private static final int REQUEST_EXTERNAL = 0;
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Log.i(TAG,
"Displaying permission rationale to provide additional context.");
Snackbar.make(mLayout, R.string.permission_camera_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL);
}
})
.show();
} else {
// permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL);
}
}
检查此链接:https://developer.android.com/training/permissions/requesting
这是Neither user 10102 nor current process has android.permission.READ_PHONE_STATE