我正在我的应用程序中实现下载管理器我想在下载管理器下载后打开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);
}
}
<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>
答案 0 :(得分:0)
我收到同样的错误,但是有一个视频文件。
下载管理器根据文件的mime类型打开文件。 您应该检查MIME类型是 application / pdf 而不是application / octet-stream或其他
答案 1 :(得分:0)
只是添加到@ rodeleon的答案
少数设备中的Download Manager根据文件的MIME类型打开文件。
有时会知道MIME类型,但由于长度,下载不成功
工作案例:长度:485449(474K)[申请/ pdf]
而不是:长度:未指定[application / octet-stream]或长度:未指定[application / pdf]
我们可以使用wget获取url的详细信息: wget url