package com.example.shery.youtubevideodownloader;
import android.Manifest;
import android.app.DownloadManager;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.*;
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);
}
});
}
}
即使我已经对manifest.xml添加了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我有一个webview应用程序,我想从中下载视频,并且在上面的代码中添加了下载功能,因此我在清单中添加了权限,但是
当我按下下载按钮时,我的应用程序崩溃了
这是我的日志“禁止写入/ storage / emulated / 0 / Download / Download:用户10143或当前进程都没有android.permission.WRITE_EXTERNAL_STORAGE”
那么如何获得运行时权限
答案 0 :(得分:0)
首先,您必须使用以下代码检查您是否已获得许可:
if (ActivityCompat.checkSelfPermission(context, Manifest.permission. WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
//Ask for permission
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
}else{
// Do your work
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);
}
});
}
现在,如果授予了权限,则必须重写此方法才能知道权限的状态。
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Do your work here.
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);
}
});
}
}
}
希望这会有所帮助。