Better handling image from another apps
This commit is contained in:
parent
69d7f9f741
commit
9492f2f91c
@ -10,8 +10,8 @@ android {
|
|||||||
applicationId "net.crystalyx.setaswallpaper"
|
applicationId "net.crystalyx.setaswallpaper"
|
||||||
minSdkVersion 23
|
minSdkVersion 23
|
||||||
targetSdkVersion 30
|
targetSdkVersion 30
|
||||||
versionCode 5
|
versionCode 6
|
||||||
versionName "2.3"
|
versionName "2.4"
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
@ -31,7 +31,7 @@ public class SetWallpaperActivity extends Activity {
|
|||||||
if ("text/plain".equals(type)) {
|
if ("text/plain".equals(type)) {
|
||||||
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
|
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
|
||||||
} else if (type.startsWith("image/")) {
|
} else if (type.startsWith("image/")) {
|
||||||
launchWallpaperActivity(intent.getParcelableExtra(Intent.EXTRA_STREAM), type);
|
handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM), type);
|
||||||
} else {
|
} else {
|
||||||
toBeFinished = true;
|
toBeFinished = true;
|
||||||
}
|
}
|
||||||
@ -83,11 +83,27 @@ public class SetWallpaperActivity extends Activity {
|
|||||||
|
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
File outputFile = File.createTempFile(generateRandomString(), generateRandomString(), getCacheDir());
|
|
||||||
URLConnection connection = url.openConnection();
|
URLConnection connection = url.openConnection();
|
||||||
connection.connect();
|
connection.connect();
|
||||||
|
|
||||||
InputStream inputStream = connection.getInputStream();
|
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);
|
OutputStream outputStream = new FileOutputStream(outputFile);
|
||||||
|
|
||||||
byte[] data = new byte[1024];
|
byte[] data = new byte[1024];
|
||||||
@ -101,12 +117,7 @@ public class SetWallpaperActivity extends Activity {
|
|||||||
outputStream.close();
|
outputStream.close();
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
|
|
||||||
Uri uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile);
|
return FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile);
|
||||||
launchWallpaperActivity(uri, connection.getContentType());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void launchWallpaperActivity(Uri uri, String mimeType) {
|
private void launchWallpaperActivity(Uri uri, String mimeType) {
|
||||||
|
Loading…
Reference in New Issue
Block a user