2016-10-12 9 views
0

DownloadManagerを使用してpdfをダウンロードしようとしています。ダウンロードマネージャーを使用している前のロリポップデバイスでpdfがダウンロードされない

リンクにパラメータを渡しており、生成され、デバイスにダウンロードされたサーバーpdfファイルから渡しています。ので、私のダウンロードリンクは以下のようになり、いくつかのものをこの

http://example.com/gen.php?data={......}

私のコードのように:

final Request request = new Request(Uri.parse(fromUrl)); 
request.setMimeType("application/pdf"); 
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
request.setDestinationInExternalPublicDir(path, "ELALA_MANIFEST_" + System.currentTimeMillis() + ".pdf"); 

final DownloadManager dm = (DownloadManager) context 
     .getSystemService(Context.DOWNLOAD_SERVICE); 
try { 
    try { 
     dm.enqueue(request); 
    } catch (SecurityException e) { 
     request.setNotificationVisibility(Request.VISIBILITY_VISIBLE); 
     Log.e("Error", e.toString()); 
     dm.enqueue(request); 
    } 

} 
// if the download manager app has been disabled on the device 
catch (IllegalArgumentException e) { 
    openAppSettings(context, 
      AdvancedWebView.PACKAGE_NAME_DOWNLOAD_MANAGER); 
} 

このは、上記のAPIレベル21(ロリポップ)で、デバイスに正常に動作します。下のバージョンではpdfはダウンロードされません。通知は、私は何かがsetMimeTypeに関係していますが、何を知らないことを知っている、などの

<untitled> download unsuccessful 

をポップ。

ご協力いただきますようお願い申し上げます。

+0

その他のファイルタイプがあなたの前ロリポップで正常に動作しますか? –

+0

@KamranAhmedはい... –

+0

あなたはDownloadManagerを取得することができますデータベースを 'adb root && adb pull /data/data/com.android.providers.downloads/databases/downloads.db 'とします。 –

答えて

1

あなたが<untitled> download unsuccessfulを取得すると、通常、あなたのURLが正しくないか、空であるか、またはnullであることを意味します。だからまず第一に、URLがOKであることを確認してください。

4.4(pre-lolipop)で動作するDownloadManagerで私の小さな例を見てください。

import android.app.DownloadManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.text.TextUtils; 
import android.util.Log; 
import android.view.View; 

import java.io.File; 

public class MainActivity extends AppCompatActivity { 

    private DownloadManager dm; 
    private String url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
     url = "https://collegereadiness.collegeboard.org/pdf/psat-nmsqt-practice-test-1.pdf"; 

     findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (TextUtils.isEmpty(url)) { 
        throw new IllegalArgumentException("url cannot be empty or null"); 
       } 

       DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

       if (isExternalStorageWritable()) { 
        String uriString = v.getContext().getExternalFilesDir(null) + ""; 
        File file = new File(uriString, Uri.parse(url).getLastPathSegment()); 
        Uri destinationUri = Uri.fromFile(file); 
        request.setDestinationUri(destinationUri); 
        dm.enqueue(request); 
       } 
      } 
     }); 

     registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
    } 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
       Log.d(TAG, "onReceive() returned: " + action); 
       // TODO: 2016-10-12 
      } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) { 
       Log.d(TAG, "onReceive() returned: " + action); 
       // TODO: 2016-10-12 
      } 
     } 
    }; 

    private static final String TAG = "MainActivity"; 

    //... 

    public boolean isExternalStorageWritable() { 
     String state = Environment.getExternalStorageState(); 
     return Environment.MEDIA_MOUNTED.equals(state); 
    } 

    public boolean isExternalStorageReadable() { 
     String state = Environment.getExternalStorageState(); 
     return Environment.MEDIA_MOUNTED.equals(state) || 
       Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); 
    } 
} 

これは、権限を使用しています。

<uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

すべての条件を処理することが重要です。また、アプリディレクトリを使用して、代わりにあなたのsdカードを回避してみてください。この例では、ダウンロードしたファイルを /sdcard/Android/data/{your app package name}/files/に保存します。以前は、sdcardがマウントされているかどうか、ディレクトリに書き込めるかどうかを確認しました。

サンプル:

enter image description here


オーケーは、ここでヘッダは、「コンテンツDisposition`フィールドを持っている場合は、ファイル名を解決すると私のサンプルであるあなたはそれがあなたのサーバー上にある、あなたができるようになりますので。それ:)

import android.app.DownloadManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.text.TextUtils; 
import android.util.Log; 
import android.view.View; 

import java.io.File; 
import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class MainActivity extends AppCompatActivity { 

    private DownloadManager dm; 
    private String url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
     url = "http://dl1.shatelland.com/files/07610a8d-a73f-45bb-8868-6fd33299bda7/6e33639f-0ce0-43ef-86e4-43492db1be86"; 

     findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(final View v) { 

       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         downloadFileInTask(v.getContext(), url); 
        } 
       }).start(); 
      } 
     }); 

     registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
    } 

    private void downloadFileInTask(Context v, String url) { 
     if (TextUtils.isEmpty(this.url)) { 
      throw new IllegalArgumentException("url cannot be empty or null"); 
     } 

     /*when redirecting from hashed url and found headerField "Content-Disposition"*/ 
     String resolvedFile = resolveFile(url, "unknown_file"); 

     DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

     if (isExternalStorageWritable()) { 
      File file = new File(v.getExternalFilesDir(null), resolvedFile); 
      Uri destinationUri = Uri.fromFile(file); 
      request.setDestinationUri(destinationUri); 
      dm.enqueue(request); 
     } 
    } 

    private String resolveFile(String url, String defaultFileName) { 
     String filename = defaultFileName; 

     HttpURLConnection con = null; 
     try { 
      con = (HttpURLConnection) new URL(url).openConnection(); 
      con.setInstanceFollowRedirects(true); 
      con.connect(); 

      String contentDisposition = con.getHeaderField("Content-Disposition"); 
      if (!TextUtils.isEmpty(contentDisposition)) { 
       String[] splittedCD = contentDisposition.split(";"); 
       for (int i = 0; i < splittedCD.length; i++) { 
        if (splittedCD[i].trim().startsWith("filename=")) { 
         filename = splittedCD[i].replaceFirst("filename=", "").trim(); 
         break; 
        } 
       } 
      } 

      con.disconnect(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return filename; 
    } 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
       Log.d(TAG, "onReceive() returned: " + action); 
       // TODO: 2016-10-12 
      } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) { 
       Log.d(TAG, "onReceive() returned: " + action); 
       // TODO: 2016-10-12 
      } 
     } 
    }; 

    private static final String TAG = "MainActivity"; 

    //... 

    public boolean isExternalStorageWritable() { 
     String state = Environment.getExternalStorageState(); 
     return Environment.MEDIA_MOUNTED.equals(state); 
    } 

    public boolean isExternalStorageReadable() { 
     String state = Environment.getExternalStorageState(); 
     return Environment.MEDIA_MOUNTED.equals(state) || 
       Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); 
    } 
} 
+0

返信ありがとうございます。しかし私は私のケースです。pdfはサーバーのどこかに保存されていません。それはパラメータiが通過することによって生成される。私のダウンロードリンクはこのようなものです。http://example.com/gen.php?data= {......} –

+0

ありがとう、ありがとう、私は私の答えを編集しようとします。質問を作成するときは、より具体的にしてください。 :) – deadfish

+0

大丈夫..ありがとう..あなたの心配です。 –

関連する問題