Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29741

Download HTTP resource doesn't work on android9 using DownloadManager

$
0
0

I have an react-native app runs on native android9, now I want to use DownloadManager to download a file, it's a http resource. when download is begining, it doesn't show notification immediately, no downloading progress too, and waiting for many minutes it shows download failed. The DownloadManager.COLUMN_REASON is 1004, according the doc it means ERROR_HTTP_DATA_ERROR. I have set networkSecurityConfig in AndroidManifest.xml. If I change the file to a https resource, it works fine, but there still have a problem that it also doesn't show downloading progress in notification bar untill download completed. I have set NotificationVisibility to VISIBILITY_VISIBLE_NOTIFY_COMPLETED;AndroidManifest.xml

...<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/><application  android:name=".MainApplication"  android:label="@string/app_name"  android:icon="@mipmap/ic_launcher"  android:roundIcon="@mipmap/ic_launcher_round"  android:allowBackup="false"  android:networkSecurityConfig="@xml/network_security_config"  android:theme="@style/AppTheme"><uses-library    android:name="org.apache.http.legacy"    android:required="false" /><activity    android:name=".MainActivity"    ...><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><provider      android:name="androidx.core.content.FileProvider"      android:authorities="${applicationId}.fileprovider"      android:grantUriPermissions="true"      android:exported="false"><meta-data          android:name="android.support.FILE_PROVIDER_PATHS"          android:resource="@xml/file_paths" /></provider><receiver android:name="com.cityvoice.cubebrain.MyBroadcastReceiver"><intent-filter><action android:name="android.intent.action.DOWNLOAD_COMPLETE"/></intent-filter></receiver></application>

...file_paths.xml

<?xml version="1.0" encoding="utf-8"?><resources><paths><external-files-path path="" name="Download" /></paths></resources>

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?><network-security-config><base-config cleartextTrafficPermitted="true"/></network-security-config>

AndroidDownloadManager.java

public class AndroidDownloadManager {    ...    public AndroidDownloadManager(Context context, String url, String name) {        this.context = context;        this.url = url;        this.name = name;    }    public AndroidDownloadManager setListener(AndroidDownloadManagerListener listener) {        this.listener = listener;        return this;    }    public void download() {        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));        request.setAllowedOverRoaming(false);        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);        request.setTitle(name);        request.setDescription("downloading......");        request.setVisibleInDownloadsUi(true);        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), name);        request.setDestinationUri(Uri.fromFile(file));        path = file.getAbsolutePath();        if (downloadManager == null) {            downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);        }        if (downloadManager != null) {            if (listener != null) {                listener.onPrepare();            }            downloadId = downloadManager.enqueue(request);        }        context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    }    private MyBroadcastReceiver receiver = new MyBroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            DownloadManager.Query query = new DownloadManager.Query();            Log.d(TAG, "onReceive >>>>"+ url);            query.setFilterById(downloadId);            Cursor cursor = downloadManager.query(query);            if (cursor.moveToFirst()) {                int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));                switch (status) {                    case DownloadManager.STATUS_PAUSED:                        break;                    case DownloadManager.STATUS_PENDING:                        break;                    case DownloadManager.STATUS_RUNNING:                        break;                    case DownloadManager.STATUS_SUCCESSFUL:                        if (listener != null) {                            listener.onSuccess(path);                        }                        cursor.close();                        context.unregisterReceiver(receiver);                        break;                    case DownloadManager.STATUS_FAILED:                        if (listener != null) {                            listener.onFailed(new Exception("failed"));                        }                        cursor.close();                        context.unregisterReceiver(receiver);                        break;                }            }        }    };}

Viewing all articles
Browse latest Browse all 29741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>