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,32 +83,43 @@ 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();
|
||||||
OutputStream outputStream = new FileOutputStream(outputFile);
|
launchWallpaperActivity(writeImageToCache(inputStream), connection.getContentType());
|
||||||
|
|
||||||
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());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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) {
|
private void launchWallpaperActivity(Uri uri, String mimeType) {
|
||||||
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
|
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
|
||||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||||
|
Loading…
Reference in New Issue
Block a user