提问人:BOUCHEFIRAT Loukmane 提问时间:5/12/2023 更新时间:5/12/2023 访问量:13
当我尝试从本地服务器下载时发生无标题下载
Untitled download occure when I try to download from a local server
问:
我运行一个 android 应用程序,让我从 URL 下载文件,我使用下载管理器下载文件,当我使用来自 Internet 的公共 URL 服务器时,它可以正常工作,但是当我使用本地服务器时,它不会下载文件并给我“无标题下载”,这是我的 MainActivity 和 Androidmanifest 文件, 我该如何解决这个问题?
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button downloadButton1, downloadButton2, downloadButton3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadButton1 = findViewById(R.id.button_download1);
downloadButton2 = findViewById(R.id.button_download2);
downloadButton3 = findViewById(R.id.button_download3);
downloadButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadFile("https://speed.hetzner.de/100MB.bin", "100MB.bin");
}
});
downloadButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadFile("https://192.168.101.2:3000/HI.bin", "HI.bin");
}
});
}
private void downloadFile(String url, String fileName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES, fileName);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
}
}
type here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.3downloads"
tools:targetApi="33"
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答: 暂无答案
评论