From 9492f2f91c2ebc7a32088dddf76bb664e6d73b92 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Thu, 9 Sep 2021 18:16:34 +0200 Subject: [PATCH] Better handling image from another apps --- app/build.gradle | 4 +- .../setaswallpaper/SetWallpaperActivity.java | 47 ++++++++++++------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 18689b3..6acd48f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "net.crystalyx.setaswallpaper" minSdkVersion 23 targetSdkVersion 30 - versionCode 5 - versionName "2.3" + versionCode 6 + versionName "2.4" } buildTypes { diff --git a/app/src/main/java/net/crystalyx/setaswallpaper/SetWallpaperActivity.java b/app/src/main/java/net/crystalyx/setaswallpaper/SetWallpaperActivity.java index d6309c0..4a7b8c2 100644 --- a/app/src/main/java/net/crystalyx/setaswallpaper/SetWallpaperActivity.java +++ b/app/src/main/java/net/crystalyx/setaswallpaper/SetWallpaperActivity.java @@ -31,7 +31,7 @@ public class SetWallpaperActivity extends Activity { if ("text/plain".equals(type)) { handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT)); } else if (type.startsWith("image/")) { - launchWallpaperActivity(intent.getParcelableExtra(Intent.EXTRA_STREAM), type); + handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM), type); } else { toBeFinished = true; } @@ -83,32 +83,43 @@ public class SetWallpaperActivity extends Activity { executor.execute(() -> { try { - File outputFile = File.createTempFile(generateRandomString(), generateRandomString(), getCacheDir()); URLConnection connection = url.openConnection(); connection.connect(); - InputStream inputStream = connection.getInputStream(); - OutputStream outputStream = new FileOutputStream(outputFile); - - byte[] data = new byte[1024]; - int count; - - while ((count = inputStream.read(data)) != -1) { - outputStream.write(data, 0, count); - } - - outputStream.flush(); - outputStream.close(); - inputStream.close(); - - Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile); - launchWallpaperActivity(uri, connection.getContentType()); + launchWallpaperActivity(writeImageToCache(inputStream), connection.getContentType()); } catch (IOException e) { e.printStackTrace(); } }); } + private void handleStream(Uri uri, String type) { + try { + InputStream inputStream = getContentResolver().openInputStream(uri); + launchWallpaperActivity(writeImageToCache(inputStream), type); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private Uri writeImageToCache(InputStream inputStream) throws IOException { + File outputFile = File.createTempFile(generateRandomString(), generateRandomString(), getCacheDir()); + OutputStream outputStream = new FileOutputStream(outputFile); + + byte[] data = new byte[1024]; + int count; + + while ((count = inputStream.read(data)) != -1) { + outputStream.write(data, 0, count); + } + + outputStream.flush(); + outputStream.close(); + inputStream.close(); + + return FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile); + } + private void launchWallpaperActivity(Uri uri, String mimeType) { Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); intent.addCategory(Intent.CATEGORY_DEFAULT);