Reputation: 11
I run an android app that let me download file from an URL, I used download manager to download the files, it works just fine when I use a public URL server from the internet but when I use my local server it doesn't download the file and give me "Untitled download", here's my MainActivity and Androidmanifest files, how can I fix that ?
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>
Upvotes: 1
Views: 50