Android.Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<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" />
<!-- <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>-->
<application
android:requestLegacyExternalStorage="true"
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<!-- <meta-data android:name="firebase_analytics_collection_deactivated" android:value="@bool/FIREBASE_ANALYTICS_DEACTIVATED"/> -->
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/ic_notification"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="owsapp"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.guerrilla.innov8coworking;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.defaults.DefaultReactActivityDelegate;
import android.os.Bundle;
import org.devio.rn.splashscreen.SplashScreen;
import android.Manifest;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.Arrays;
public class MainActivity extends ReactActivity {
private static final int REQUEST_WRITE_STORAGE = 0;
private String downloadUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
final boolean setFullScreen = true;
Log.d("Jugni", "Hii I am on create method");
SplashScreen.show(this, R.style.SplashScreenTheme, setFullScreen);
super.onCreate(savedInstanceState);
// Check and request permissions when the app starts
if (Build.VERSION.SDK_INT >= 23) {
Log.d("Jugni", "Hii I am inside version 24" + String.valueOf(Build.VERSION.SDK_INT));
Log.d("Jugni", "Hii my permission is" + String.valueOf(Manifest.permission.WRITE_EXTERNAL_STORAGE));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d("Jugni", "Hii my permission is" + String.valueOf(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)));
Log.d("Jugni", "Hii I am inside check self permission");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
Log.d("Jugni", "Hii my new permission is" + String.valueOf(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)));
Log.d("Jugni", "Request permission completed");
} else {
Log.d("Jugni", "Permission already granted");
}
}
}
private void downloadFile(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle("Vertretungsplan");
request.setDescription("wird heruntergeladen");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String filename = URLUtil.guessFileName(url, null, MimeTypeMap.getFileExtensionFromUrl(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.d("Jugni", "Inside Request permission block, requestCode: " + requestCode + ", permissions: " + Arrays.toString(permissions) + ", grantResults: " + Arrays.toString(grantResults));
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_WRITE_STORAGE) {
Log.d("Jugni", "GrantResults length" + String.valueOf(grantResults.length));
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("Jugni", String.valueOf( grantResults[0]));
Log.d("Jugni", downloadUrl);
// Permission granted, proceed with the file download
if (downloadUrl != null) {
downloadFile(downloadUrl);
}
} else {
// Permission denied, handle accordingly
}
}
}
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "ows_app_rn";
}
/**
* Returns the instance of the {@link ReactActivityDelegate}. Here we use a util class {@link
* DefaultReactActivityDelegate} which allows you to easily enable Fabric and Concurrent React
* (aka React 18) with two boolean flags.
*/
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new DefaultReactActivityDelegate(
this,
getMainComponentName(),
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
DefaultNewArchitectureEntryPoint.getFabricEnabled());
}
}
Build.gradle
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 24
compileSdkVersion = 33
targetSdkVersion = 34
androidXCore = "1.0.2"
// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
ndkVersion = "23.1.7779620"
}
My app doesn’t ask for file permission even after declaring and requesting it. I also tried with MANAGE_EXTERNAL_STORAGE and still faced the same issue. I read somewhere that WRITE_EXTERNAL_STORAGE has been deprecated for Android 14. Can someone tell the resolution. I want to access pdf file, no audio, video or images
New contributor
Saumya Chirania is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.