Use AndroidX photo picker, if enabled

This commit is contained in:
M66B 2023-04-25 08:17:29 +02:00
parent ab1a060c1d
commit 7ddc34f45a
2 changed files with 33 additions and 14 deletions

View File

@ -107,6 +107,9 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
@ -331,6 +334,8 @@ public class FragmentCompose extends FragmentBase {
private static final int REQUEST_SEND = 16;
private static final int REQUEST_REMOVE_ATTACHMENTS = 17;
ActivityResultLauncher<PickVisualMediaRequest> pickImages;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -355,6 +360,16 @@ public class FragmentCompose extends FragmentBase {
setTitle(R.string.page_compose);
setSubtitle(getResources().getQuantityString(R.plurals.page_message, 1));
int max = (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
? Integer.MAX_VALUE
: MediaStore.getPickImagesMaxLimit());
pickImages =
registerForActivityResult(new ActivityResultContracts.PickMultipleVisualMedia(max), uris -> {
if (!uris.isEmpty())
onAddImageFile(uris, false);
});
}
@Override
@ -3379,17 +3394,15 @@ public class FragmentCompose extends FragmentBase {
}
}
} else {
// https://developer.android.com/training/data-storage/shared/photopicker#device-availability
// https://developer.android.com/reference/android/provider/MediaStore#ACTION_PICK_IMAGES
// Android 12: cmd device_config put storage_native_boot picker_intent_enabled true
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean photo_picker = prefs.getBoolean("photo_picker", false);
Intent picker = new Intent(MediaStore.ACTION_PICK_IMAGES);
picker.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
photo_picker && picker.resolveActivity(pm) != null) {
if (photo_picker) {
Log.i("Using photo picker");
picker.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, MediaStore.getPickImagesMaxLimit());
startActivityForResult(picker, REQUEST_IMAGE_FILE);
pickImages.launch(new PickVisualMediaRequest.Builder()
.setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
.build());
} else {
Log.i("Using file picker");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

View File

@ -70,6 +70,7 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.os.TransactionTooLargeException;
import android.os.ext.SdkExtensions;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
@ -3127,13 +3128,18 @@ public class Log {
size += write(os, "\r\n");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Map<Integer, Integer> exts = SdkExtensions.getAllExtensionVersions();
for (Integer ext : exts.keySet())
size += write(os, String.format("Extension %d / %d\r\n", ext, exts.get(ext)));
if (exts.size() > 0)
size += write(os, "\r\n");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
try {
Map<Integer, Integer> exts = SdkExtensions.getAllExtensionVersions();
for (Integer ext : exts.keySet())
size += write(os, String.format("Extension %d / %d\r\n", ext, exts.get(ext)));
if (exts.size() > 0)
size += write(os, "\r\n");
size += write(os, String.format("Max. pick images: %d\r\n\r\n", MediaStore.getPickImagesMaxLimit()));
} catch (Throwable ex) {
size += write(os, String.format("%s\r\n", ex));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {