需要通过应用程序从web服务器下载文件,但该文件没有加载,下面是我使用的代码:
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
long reference = manager.enqueue(request);
在清单中,我有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
请尝试一下这类代码的方式可能会对你有所帮助
public class DownloadHelper {
private long downloadReference = 0
private DownloadManager downloadManager;
DownloadCompleteListener completeListener= null;
public interface DownloadCompleteListener{
void ondownloadComplete(String name,String path,Uri uri);
void ondownloadFailled(String error);
}
private BroadcastReceiver receiver = new BroadcastReceiver () {
@Override
void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadId != downloadReference) {
context.unregisterReceiver(this);
return;
}
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadReference);
Cursor cursor = downloadManager.query(query);
if(cursor != null){
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) {
String localFile = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
String localName = localFile.substring(localFile.lastIndexOf("/")+1);//cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME))
completeListener.ondownloadComplete(localName,localFile,null);
} else if (DownloadManager.STATUS_FAILED == cursor.getInt(columnIndex)) {
completeListener.ondownloadFailled(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)));
}
}else{
completeListener.ondownloadFailled("Download cancelled or failled");
}
cursor.close();
}else{
completeListener.ondownloadFailled("Download cancelled or failled");
}
context.unregisterReceiver(this);
}
}
};
public String getallreadyfile(String name){
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath().toString() + "/" + name;
}
public void downloadFile(Context context,String url,String mimeType,DownloadCompleteListener completeListener) {
this.completeListener = completeListener;
String guessFileName = URLUtil.guessFileName(url, null, mimeType);
if(url == null || url.isEmpty()){
return;
}
String allreadyfile = getallreadyfile(guessFileName);
File applictionFile = new File(allreadyfile);
if(isAllreadyAvailabel(context,applictionFile)){
completeListener.ondownloadComplete(applictionFile.getName(),applictionFile.getAbsolutePath(),null);
return
}
downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE).setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//setAllowedOverRoaming(true)
// setTitle(guessFileName)
// setDescription(guessFileName)
// setVisibleInDownloadsUi(true)
request.allowScanningByMediaScanner();
// setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
//request.setDestinationUri(Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)))
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, guessFileName);
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
downloadReference = downloadManager.enqueue(request);
}
public boolean isAllreadyAvailabel(Context context,File applictionFile){
if(applictionFile.canRead() && applictionFile.length()>0) {
return true;
}else{
try {
File file = new File(applictionFile.getAbsolutePath());
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
我有google drive sdk快速启动android启动和运行。 知道为什么会这样吗?
我是GCS的新手。 这里我写了几行。 现在棘手的部分是按原样下载此数据<代码>Ex.new\u文件。csv 有人解决过吗?如果那样的话,我会很感激的。 谢谢
我试图了解使用android应用程序下载文件的不同方法的利弊。 我找到了几种方法: 自己管理下载(请参阅此代码片段) 使用Android的DownloadManager 使用OKHttp下载文件,如本文所述 有这方面经验的人能告诉我选择一种方式而不是另一种方式的原因吗?
我已经创建了一个webview,并向其中添加了下载监听器。当我在应用程序中下载文件时,它将文件存储在应用程序包中,而不是公共下载文件夹中。
出于某种原因,我真的很挣扎,希望有人能帮我指明正确的方向。 我的目标是Android11/API30,这似乎是所有问题的根源。针对较低的目标可能对我有用——但谷歌似乎最终会迫使我走上这条路,所以我还是想办法解决这个问题。 我的应用程序通常按照标准写入文件 这给了我一个设备上的路径 我还尝试了其他类型,比如: 结果在 我的应用程序没有任何问题实际上写文件到这个位置。在Android11中,我必须用本
编写一个简单的spring程序从filepathxmlapplication上下文中读取bean,但得到以下异常。 但我确信在可以访问xml bean定义文件。