Better handling image from another apps
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Michel Roux 2021-09-09 18:16:34 +02:00
parent 69d7f9f741
commit 9492f2f91c
2 changed files with 31 additions and 20 deletions

View File

@ -10,8 +10,8 @@ android {
applicationId "net.crystalyx.setaswallpaper"
minSdkVersion 23
targetSdkVersion 30
versionCode 5
versionName "2.3"
versionCode 6
versionName "2.4"
}
buildTypes {

View File

@ -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,11 +83,27 @@ 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();
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];
@ -101,12 +117,7 @@ public class SetWallpaperActivity extends Activity {
outputStream.close();
inputStream.close();
Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile);
launchWallpaperActivity(uri, connection.getContentType());
} catch (IOException e) {
e.printStackTrace();
}
});
return FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile);
}
private void launchWallpaperActivity(Uri uri, String mimeType) {