下载管理器下载后 PDF 文件打不开
Posted
技术标签:
【中文标题】下载管理器下载后 PDF 文件打不开【英文标题】:PDF file is not getting open after downloading by Download Manager 【发布时间】:2014-05-13 04:07:22 【问题描述】:我正在我的应用程序中实现下载管理器,我想在下载管理器下载后打开 pdf 文件。文件正在下载但未打开。我无法弄清楚我下面的书面代码有什么问题。
import in.b.app.constant.BConstant;
import in.b.app.databasemanagement.SDatabaseHandler;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AnnouncementDetailsActivity extends Activity implements
OnClickListener
private static String nid;
private static String announcementTitle;
private static String announcementDetails;
private static String announcementCreatedDate;
private static String cookie;
private static String token;
SharedPreferences sharedPreferences;
TextView title;
TextView details;
TextView createdDate;
TextView createdMonth;
Typeface tf;
Typeface announcementDetailsFont;
ArrayList<String> returnsAnnouncementFilesids = new ArrayList<String>();
SDatabaseHandler sDatabaseHandler;
Button downloadFile;
private long lastDownload = -1L;
private DownloadManager downloadManager = null;
@SuppressLint("SimpleDateFormat")
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.announcement_details_page);
downloadFile = (Button) findViewById(R.id.get_file);
tf = Typeface.createFromAsset(this.getAssets(),
BConstant.FONTS_GOTHAM_BOLD_WEBFONT);
sDatabaseHandler = new SDatabaseHandler(this);
announcementDetailsFont = Typeface.createFromAsset(this.getAssets(),
BConstant.FONTS_GOTHAM_BOOK_WEBFONT);
sharedPreferences = getSharedPreferences(
BConstant.B_LOGIN_CHECK, BConstant.PRIVATE_MODE);
cookie = sharedPreferences.getString(
BConstant.WEB_SERVICES_COOKIES, "");
token = sharedPreferences.getString(BConstant.TOKEN, "");
Intent intent = getIntent();
nid = intent.getStringExtra(BConstant.PRODUCT_NODE_ID);
announcementTitle = intent
.getStringExtra(BConstant.ANNOUNCEMENT_TITLE);
announcementDetails = intent
.getStringExtra(BConstant.ANNOUNCEMENT_DETAILS);
announcementCreatedDate = intent
.getStringExtra(BConstant.CREATED_DATE);
returnsAnnouncementFilesids = sDatabaseHandler
.getAnnouncementURLsFID(nid);
// File file = new File(getExternalFilesDir("Rupesh") + "Rupesh.png");
///IntentFilter filter = new IntentFilter();
// filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
// filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
/////filter.addAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
//registerReceiver(onComplete, filter);
//registerReceiver(onNotificationClick, filter);
downloadManager=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
registerReceiver(onNotificationClick,
new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
createdDate = (TextView) findViewById(R.id.getAnnouncementDate);
title = (TextView) findViewById(R.id.getAnnouncementName);
details = (TextView) findViewById(R.id.getAnnouncementDetails);
createdMonth = (TextView) findViewById(R.id.getMonth);
title.setTypeface(tf);
details.setTypeface(announcementDetailsFont);
long cDate = Long.parseLong(announcementCreatedDate);
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(cDate * 1000L);
Date d = c.getTime();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("dd");
SimpleDateFormat simpleMonthformat = new SimpleDateFormat("MMM");
String date = simpleDateformat.format(d);
String month = simpleMonthformat.format(d);
createdDate.setText(date);
createdMonth.setText(month);
title.setText(announcementTitle);
details.setText(Html.fromHtml(announcementDetails));
downloadFile.setOnClickListener(this);
@Override
public void onBackPressed()
super.onBackPressed();
overridePendingTransition(R.anim.right_in_details,
R.anim.right_out_left_in_details);
BroadcastReceiver onComplete = new BroadcastReceiver()
public void onReceive(Context ctxt, Intent intent)
Toast.makeText(ctxt, "File Downloaded Successfully!!",
Toast.LENGTH_LONG).show();
;
BroadcastReceiver onNotificationClick = new BroadcastReceiver()
public void onReceive(Context ctxt, Intent intent)
Toast.makeText(ctxt, "File Downloaded Successfully!!",
Toast.LENGTH_LONG).show();
;
public void viewDownload()
Intent mView = new Intent();
mView.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(mView);
@Override
public void onDestroy()
super.onDestroy();
unregisterReceiver(onComplete);
unregisterReceiver(onNotificationClick);
@Override
protected void onPause()
super.onPause();
unregisterReceiver(onComplete);
unregisterReceiver(onNotificationClick);
@Override
public void onClick(View v)
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri
.parse("https://www.cl.cam.ac.uk/~ib249/teaching/Lecture1.handout.pdf");
DownloadManager.Request request = new DownloadManager.Request(
Download_Uri);
request.addRequestHeader(BConstant.WEB_SERVICES_COOKIES, cookie);
request.addRequestHeader(BConstant.WEB_SERVICES_TOKEN_HEADER,
token);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("Downloading Attachment...");
request.setDestinationInExternalFilesDir(this, "Rupesh", "Rupesh"
+ ".png");
lastDownload = downloadManager.enqueue(request);
// v.setEnabled(false);
ManiFest 文件:
<activity
android:name="in.b.app.AnnouncementDetailsActivity"
android:label="@string/app_name"
android:noHistory="true"
android:parentActivityName="in.b.app.HomePageActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.pdf" />
</intent-filter>
</activity>
【问题讨论】:
【参考方案1】:只是为了添加到@rodeleon 的答案。 少数设备中的下载管理器会根据文件的 MIME 类型打开基于文件长度的文件。 有时会知道 MIME 类型,但由于长度原因下载不成功。 工作案例:长度:485449 (474K) [应用程序/pdf] 而不是:长度:未指定 [application/octet-stream] 或长度:未指定 [application/pdf]
我们可以使用 wget 获取 url 的详细信息: wget 网址
【讨论】:
【参考方案2】:我遇到了同样的错误,但有一个视频文件。
下载管理器根据文件的 mime 类型打开文件。 您应该检查 MIME 类型是否为 application/pdf 而不是 application/octet-stream 或其他内容
【讨论】:
感谢您回答我已经找到解决方案的问题。以上是关于下载管理器下载后 PDF 文件打不开的主要内容,如果未能解决你的问题,请参考以下文章