我正在尝试使用DownloadManager
下载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没有下载。和通知弹出类似,
<untitled> download unsuccessful
我知道这与setMimeType
有关,但不知道是什么。
任何帮助将不胜感激。
答案 0 :(得分:1)
当你得到colnames(dataFrame) <- c("firstCol", "secondCol")
时,通常意味着你的网址不正确或空或空。首先请确保网址没问题。
使用DownloadManager运行4.4(pre-lolipop)中的小例子。
<untitled> download unsuccessful
它使用权限:
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);
}
}
处理所有条件非常重要。也可以尝试使用app目录,而不是乱搞你的SD卡。此示例将下载的文件保存在
<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卡,以及我是否可以在目录上写入。
样品:
好的,如果标题有&#34; Content-Disposition`字段,这是我的解析文件名的示例。你说它是在你的服务器上,所以你将能够做到这一点:)
/sdcard/Android/data/{your app package name}/files/