X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fasynctasks%2FSaveAboutVersionImage.java;h=1c15bc9c56c9dea22c4097509818d5eb1e2ec7e8;hp=377ce7be249e6dd0a5e5daba6b02cd8d91f25d3c;hb=d53f0640263cf0799e7304fa459864c542ab0d2a;hpb=c2350d6be15259bc560c623a09b9ac98cca1c876 diff --git a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveAboutVersionImage.java b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveAboutVersionImage.java index 377ce7be..1c15bc9c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveAboutVersionImage.java +++ b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveAboutVersionImage.java @@ -20,10 +20,13 @@ package com.stoutner.privacybrowser.asynctasks; import android.app.Activity; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Canvas; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; +import android.provider.OpenableColumns; import android.widget.LinearLayout; import com.google.android.material.snackbar.Snackbar; @@ -35,26 +38,45 @@ import java.io.OutputStream; import java.lang.ref.WeakReference; public class SaveAboutVersionImage extends AsyncTask { + // Declare the class constants. + private final String SUCCESS = "Success"; + // Declare the weak references. private final WeakReference activityWeakReference; private final WeakReference aboutVersionLinearLayoutWeakReference; - // Declare the class constants. - private final String SUCCESS = "Success"; - // Declare the class variables. private Snackbar savingImageSnackbar; private Bitmap aboutVersionBitmap; + private final Uri fileNameUri; private final String fileNameString; // The public constructor. - public SaveAboutVersionImage(Activity activity, String fileNameString, LinearLayout aboutVersionLinearLayout) { + public SaveAboutVersionImage(Activity activity, Uri fileNameUri, LinearLayout aboutVersionLinearLayout) { // Populate the weak references. activityWeakReference = new WeakReference<>(activity); aboutVersionLinearLayoutWeakReference = new WeakReference<>(aboutVersionLinearLayout); // Store the class variables. - this.fileNameString = fileNameString; + this.fileNameUri = fileNameUri; + + // Query the exact file name if the API >= 26. + if (Build.VERSION.SDK_INT >= 26) { + // Get a cursor from the content resolver. + Cursor contentResolverCursor = activity.getContentResolver().query(fileNameUri, null, null, null); + + // Move to the first row. + contentResolverCursor.moveToFirst(); + + // Get the file name from the cursor. + fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + + // Close the cursor. + contentResolverCursor.close(); + } else { + // Use the URI last path segment as the file name string. + fileNameString = fileNameUri.getLastPathSegment(); + } } // `onPreExecute()` operates on the UI thread. @@ -107,7 +129,7 @@ public class SaveAboutVersionImage extends AsyncTask { try { // Open an output stream. - OutputStream outputStream = activity.getContentResolver().openOutputStream(Uri.parse(fileNameString)); + OutputStream outputStream = activity.getContentResolver().openOutputStream(fileNameUri); // Write the webpage image to the image file. aboutVersionByteArrayOutputStream.writeTo(outputStream);