gpt4 book ai didi

android - 下载后如何在android的pdf查看器中显示并读取文件

转载 作者:行者123 更新时间:2023-11-29 02:27:08 25 4
gpt4 key购买 nike

我创建了一个应用程序,如果用户单击任何 PDF,它将下载该 PDF。现在我想在下载后显示该 PDF。我不知道如何在 onPostExecute 方法下载后显示该 pdf。文件的存储路径为Environment.getExternalStorageDirectory() + "/"+ "android"+ "/"+ "Data"+ "/"+ "foldername"+"/"+"Filename"

public class DownloadTask {
cutomealertbox cdd;
File apkStorage = null;
File outputFile = null;
private Context context;

private String downloadUrl = "", downloadFileName = "";
public DownloadTask(Context context, String downloadUrl) {
this.context = context;

this.downloadUrl = downloadUrl;
downloadFileName = downloadUrl.substring(downloadUrl.lastIndexOf('/'), downloadUrl.length());
Log.e(TAG, downloadFileName);
cdd=new cutomealertbox((Activity) context);
new DownloadingTask().execute();
}

private class DownloadingTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
cdd.show(); // cdd is alert box
}

@Override
protected void onPostExecute(Void result) {
try {
if (outputFile != null) {
cdd.dismiss();
} else {

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

}
}, 3000);

Log.e(TAG, "Cannot load please try again");

}
} catch (Exception e) {
Log.e(TAG, "Download Failed with Exception - " + e);

}

最佳答案

使用下面的方法并将上下文和本地路径作为参数传递。

public static void openFile(Context context, String localPath) {
// Create URI
try {
File file = new File(localPath);

Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file
if (file.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}


} catch (Exception e) {
e.printStackTrace();
}
}

关于android - 下载后如何在android的pdf查看器中显示并读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591406/

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