这是我用于webview和下载文件的代码 此代码不适用于我要从保存在Blob中的mysql db下载的文件 这适用于静态网址。 请帮我解决这个问题,我有点卡在这里
package com.example.da;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.DownloadManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private static final String TAG="_";
WebView daweb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
daweb = findViewById(R.id.daweb);
//daweb.getSettings().setPluginState(WebSettings.PluginState.ON);
//daweb.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
daweb.getSettings().setJavaScriptEnabled(true);
daweb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
daweb.getSettings().setSupportMultipleWindows(true);
daweb.setHorizontalScrollBarEnabled(false);
daweb.setWebChromeClient(new WebChromeClient());
daweb.loadUrl("http://192.168.43.254/da/");
daweb.setWebViewClient(new WebViewClient());
daweb.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
downloadDialog(url,userAgent,contentDisposition,mimetype);
} else {
Log.v(TAG,"Permission is revoked");
//requesting permissions.
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
else {
//Code for devices below API 23 or Marshmallow
Log.v(TAG,"Permission is granted");
downloadDialog(url,userAgent,contentDisposition,mimetype);
}
}
});
}
public void downloadDialog(final String url,final String userAgent,String contentDisposition,String mimetype)
{
//getting filename from url.
final String filename = "Diabetes_care";
//alertdialog
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//title of alertdialog
builder.setTitle("Download");
//message of alertdialog
builder.setMessage("Do you want to save " +filename);
//if Yes button clicks.
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//DownloadManager.Request created with url.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//cookie
String cookie= CookieManager.getInstance().getCookie(url);
//Add cookie and User-Agent to request
request.addRequestHeader("Cookie",cookie);
request.addRequestHeader("User-Agent",userAgent);
//file scanned by MediaScannar
request.allowScanningByMediaScanner();
//Download is visible and its progress, after completion too.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//DownloadManager created
DownloadManager downloadManager=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
//Saving files in Download folder
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
//download enqued
downloadManager.enqueue(request);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
//cancel the dialog if Cancel clicks
dialog.cancel();
}
});
//alertdialog shows.
builder.create().show();
}
}
这就是我要下载pdf的方式... 这在webview中不起作用,但在浏览器中可以正常工作
echo "<a download='diabetes_care.PDF' href='data:application/pdf;base64,".base64_encode($row[1])."'>Click here to download</a>"
这在应用程序和浏览器中均能正常工作
echo "<a download='diabetes_care.PDF' href='pdf\sample.pdf'>Click here to download</a>"