簡體   English   中英

通過下載管理器下載后,PDF文件無法打開

[英]PDF file is not getting open after downloading by Download Manager

我正在我的應用程序中實現下載管理器,我想在由下載管理器下載后打開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類型是application / pdf,而不是application / octet-stream或其他類型

只是為了增加@rodeleon的答案。
在少數設備中的下載管理器根據文件的長度以及文件的MIME類型打開文件。 有時會知道MIME類型,但由於長度的原因,下載失敗。
工作案例:長度:485449(474K)[application / pdf]
而不是:長度:未指定[應用程序/八位字節流]或長度:未指定[應用程序/ pdf流]

我們可以使用wget獲取url的詳細信息:wget url

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM