Android – Update from targetSDK 28 to 29

Can’t create file

Need to add android:requestLegacyExternalStorage=”true”

https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<application
...
android:requestLegacyExternalStorage="true">
<application ... android:requestLegacyExternalStorage="true">
    <application
        ...
        android:requestLegacyExternalStorage="true">

Can’t get hardware serial number

App crashes when calling Build.getSerial()

best-practices-android-identifiers https://developer.android.com/reference/android/os/Build#getSerial()
changes-to-device-identifiers-in.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The user 10180 does not meet the requirements to access device identifiers.
The user 10180 does not meet the requirements to access device identifiers.
The user 10180 does not meet the requirements to access device identifiers.
Use ANDROID_ID for Android 10, 11; Build.getSerial() for lower Android
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
private String getAndroidId() {
return Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
private String getAndroidId() { return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); }
    private String getAndroidId() {
        return Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
    }

Can’t download file using DownloadManager

Error when dowloading file using setDestinationInExternalPublicDir
Not one of standard directories: /packagename/files

setDestinationInExternalPublicDir

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/files"; // /storage/emulated/0/test/files
if (Build.VERSION.SDK_INT >= 29) {
// Android 10, 11
Uri dstUri = Uri.parse("file://" + storagePath + File.separator + targetFilename);
dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("File Download")
.setDestinationUri(dstUri));
} else {
dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("File Downloading...")
.setDescription("File Download")
.setDestinationInExternalPublicDir("/test/files", targetFilename));
}
String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/files"; // /storage/emulated/0/test/files if (Build.VERSION.SDK_INT >= 29) { // Android 10, 11 Uri dstUri = Uri.parse("file://" + storagePath + File.separator + targetFilename); dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle("File Downloading...") .setDescription("File Download") .setDestinationUri(dstUri)); } else { dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle("File Downloading...") .setDescription("File Download") .setDestinationInExternalPublicDir("/test/files", targetFilename)); }
String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/files"; // /storage/emulated/0/test/files

if (Build.VERSION.SDK_INT >= 29) {
    // Android 10, 11
    Uri dstUri = Uri.parse("file://" + storagePath + File.separator + targetFilename);
    dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
        .setAllowedOverRoaming(false)
        .setTitle("File Downloading...")
        .setDescription("File Download")
        .setDestinationUri(dstUri));
} else {
    dm.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
        .setAllowedOverRoaming(false)
        .setTitle("File Downloading...")
        .setDescription("File Download")
        .setDestinationInExternalPublicDir("/test/files", targetFilename));
}

Camera stops saving image

Before (Xamarin)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
takePictureIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(imageFile));
takePictureIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(imageFile));
takePictureIntent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(imageFile));

After (Xamarin)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var context = MainActivity.Instance.ApplicationContext;
var imageUri = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.PackageName + ".provider", imageFile);
takePictureIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
var context = MainActivity.Instance.ApplicationContext; var imageUri = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.PackageName + ".provider", imageFile); takePictureIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
var context = MainActivity.Instance.ApplicationContext;
var imageUri = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.PackageName + ".provider", imageFile);

takePictureIntent.PutExtra(MediaStore.ExtraOutput, imageUri);

Be the first to comment

Leave a Reply

Your email address will not be published.


*