I have migrated my project compileSdkVersion from 28 to 29 and add android:requestLegacyExternalStorage="true" in my manifest, but file operations like downloading and file opening not working as it is in SDK 28
gradle file
compileSdkVersion 29
defaultConfig {
applicationId "in.example.app"
minSdkVersion 21
targetSdkVersion 29
versionCode 90
versionName "1.8.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="in.example.app">
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SMS_FINANCIAL_TRANSACTIONS" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:theme,android:allowBackup"
tools:targetApi="n"
android:requestLegacyExternalStorage="true">
<activity android:name=".ui.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="in.example.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
file downloading code
fun startDownloading(path: String, url: String): Long {
return try {
val downFileName = re.replace(url.substringAfterLast("/"), "")
val downloadManager = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(Uri.parse(url))
request.setTitle(downFileName)
.setDestinationInExternalPublicDir(path, downFileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
downloadManager.enqueue(request)
} catch (e: Exception){
0
}
}
- path should be a folder location in internal storage "/myapp/study materials/"
- url should be a file location url from the server
opening a file
fun openFile(title: String, path: String){
try {
val newFile = File(Environment.getExternalStorageDirectory().absolutePath + path, title)
val uri = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
{
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", newFile)
} else{
Uri.fromFile(newFile)
}
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setDataAndType(uri, context.contentResolver.getType(uri))
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val activities: List<ResolveInfo> = context.packageManager.queryIntentActivities(intent, 0)
val isIntentSafe: Boolean = activities.isNotEmpty()
// Start an activity if it's safe
if (isIntentSafe) {
context.startActivity(Intent.createChooser(intent, "Open With"))
} else{
MDToast.makeText(context, "No Application Found For Opening This File", MDToast.TYPE_INFO).show()
}
} catch (e : Exception){
println("=============== ${e.message}")
}
}
- path should be a folder location in internal storage "/myapp/study materials/"
- title should be the file name
file existance checking
override fun checkFileExistence(title: String, path: String): Boolean {
var flag = false
try {
val direct = File(
Environment.getExternalStorageDirectory().toString()
+ path + title)
flag = direct.exists()
} catch (e: Exception) {
}
return flag
}
- path should be a folder location in internal storage "/myapp/study materials/"
- title should be the file name
I have added all code that is not working while updating to SDK 29, I want to download a file from the server and save it to an app-specific folder in internal storage also need to check if the file is already downloaded or not before downloading it. if it is already downloaded I need to open that file
I had similar issues to those mentioned here. I found out that on Android 10, I have to check
WRITE_EXTERNAL_STORAGE
even when file is already downloaded and I just need to open it.Adding the check before any manipulation with the file fixed the "There was a problem parsing the package" error when opening the APK file.
EDIT:
I also found another (probably better) solution. Instead of using external public storage, I switched to external storage of the app:
and to access the file use
instead of now deprecated
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
As a bonus there's no need to use
android:requestLegacyExternalStorage="true"
in AndroidManifest.