我正在我的应用程序中实现下载管理器,我想在通过下载管理器下载后打开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>
我遇到了同样的错误,但有一个视频文件。
下载管理器根据文件的 MIME 类型打开文件。您应该检查MIME类型是否为应用程序/pdf,而不是应用程序/八位字节流或其他内容
只是为了补充@rodeleon的答案。
下载管理器在少数设备中根据文件的MIME类型打开文件长度的文件。有时MIME类型是已知的,但由于长度,下载不成功。
工作案例: 长度: 485449 (474K) [应用程序/pdf]
而不是:长度:未指定 [应用程序/八位字节流] 或长度:未指定 [应用程序/pdf]
我们可以使用 wget 获取 URL 的详细信息:wget 网址