gpt4 book ai didi

android - 从 Android 网站下载

转载 作者:行者123 更新时间:2023-11-30 04:12:57 25 4
gpt4 key购买 nike

我的路径的 Url 是这样的 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);
}
}
}

最佳答案

您必须编辑 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>

关于android - 从 Android 网站下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10526538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com