Better handling image from another apps
This commit is contained in:
parent
69d7f9f741
commit
9492f2f91c
@ -10,8 +10,8 @@ android {
|
||||
applicationId "net.crystalyx.setaswallpaper"
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 30
|
||||
versionCode 5
|
||||
versionName "2.3"
|
||||
versionCode 6
|
||||
versionName "2.4"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user