我的路径的网址是这样的www.sample.com/sample/1234
。
当您单击路径时,它会下载类似此文件的文件
Sample_hello_sample.epub
我无法让我的应用程序使用此下载。
这是我的代码:
private class InsideWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("sample")) {
startDownload(url);
return true;
}
//...?
view.loadUrl(url);
return true;
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Gutenbergmain.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void startDownload(String url) {
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
if (id == DIALOG_DOWNLOAD_PROGRESS)
mProgressDialog.setProgress(0);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
mProgressDialog.setProgress(0);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
String path = url.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
String fileName = idStr;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/Sample/epub");
dir.mkdirs();
File file = new File(dir, fileName);
InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream f = new FileOutputStream(file);
//InputStream input = new BufferedInputStream(url.openStream());
//OutputStream output = new FileOutputStream("/sdcard/" + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
f.write(data, 0, count);
//output.write(data, 0, count);
}
f.flush();
f.close();
//output.flush();
//output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
答案 0 :(得分:1)
您必须编辑AndroidManifest.xml文件。例如。
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.epub" />
</intent-filter>