我创建了一个应用程序,该应用程序的运行方式类似于用户单击任何PDF都会下载该PDF。现在,我想在下载后显示该PDF。在 onPostExecute 方法下载后,我不知道如何显示该pdf。文件的存储路径为 Environment.getExternalStorageDirectory()+“ /” +“ android” +“ /” +“数据” +“ /” +“文件夹名” +“ /” +“文件名” < / p>
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);
}
答案 0 :(得分:0)
执行后,
使用Kotlin,
var extension: String? = null
val file = File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/"+ "foldername"+"/"+"Filename")
val builder = StrictMode.VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
val uri = Uri.fromFile(file)
val intent = 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 (uri.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf")
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
mContext!!.startActivity(intent)
通过使用Java
String extension = null;
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + "android" + "/" + "Data" + "/" + "foldername" + "/" + "Filename")
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
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 (uri.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
答案 1 :(得分:0)
使用以下方法并将上下文和本地路径作为参数传递。
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();
}
}
答案 2 :(得分:0)
假定您已将库集成到项目中。将此添加到您要在其中查看PDF并将XML可见性设置为GONE的XML布局中。
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
在postExecute(无效结果)方法中,根据here给出的文档设置pdf视图,并将pdfView的可见性设置为View.VISIBLE。