[英]android: download a pdf file and open after
I'm trying to download a pdf doc from internet and after that, when download ends, open it automatically throught a package. 我正在尝试从互联网上下载pdf文档,之后,当下载结束时,通过包自动打开它。
I found some solutions for downloading by DownloadManager but no answer to open it after. 我找到了一些由DownloadManager下载的解决方案,但之后无法解答。 One of the codes I've tested is: 我测试过的代码之一是:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri uri = Uri.parse(uriString);
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.xxxxxxx.org/directory/abc.pdf"));
enqueue = dm.enqueue(request);
}
but when the download ends successfully, the package don't find the file. 但是当下载成功结束时,程序包找不到该文件。
I need your help. 我需要你的帮助。 I have spent two weeks looking for a solutions via google and android books with no success. 我花了两周的时间通过谷歌和Android书籍寻找解决方案但没有成功。
Thanks. 谢谢。
Create this method and call it in your code 创建此方法并在代码中调用它
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
Intent testIntent = new Intent("com.adobe.reader");
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
Or else you can refer my answer here. 或者你可以在这里引用我的答案。
How to use Asynchronous task for download progress dialog in fragment? 如何在片段中使用异步任务进行下载进度对话框?
Its for downloading file and displaying progress diolog.Here i used asynch task.Call above method in onpost() method of asynch task. 它用于下载文件和显示进度diolog.Here我使用asynch任务。在asynt任务的onpost()方法上调用上面的方法。 Hope it helps you too 希望它也能帮到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.