Add image download by url
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
65dfee4d3e
commit
a4c874ed06
@ -8,7 +8,7 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
applicationId "net.crystalyx.setaswallpaper"
|
||||
minSdkVersion 4
|
||||
minSdkVersion 14
|
||||
targetSdkVersion 30
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
@ -27,4 +27,5 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "androidx.core:core:1.6.0"
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="net.crystalyx.setaswallpaper">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
android:label="Set as wallpaper"
|
||||
@ -15,7 +17,20 @@
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
<data android:mimeType="image/*"/>
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND"/>
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
<data android:mimeType="text/plain"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider android:authorities="net.crystalyx.setaswallpaper.provider"
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:grantUriPermissions="true"
|
||||
android:exported="false">
|
||||
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths"/>
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
@ -4,6 +4,16 @@ import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class SetWallpaperActivity extends Activity {
|
||||
|
||||
@ -12,11 +22,71 @@ public class SetWallpaperActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Intent intent = getIntent();
|
||||
if (Intent.ACTION_SEND.equals(intent.getAction())) {
|
||||
launchWallpaperActivity(intent.getParcelableExtra(Intent.EXTRA_STREAM), intent.getType());
|
||||
String type = intent.getType();
|
||||
|
||||
if (Intent.ACTION_SEND.equals(intent.getAction()) && type != null) {
|
||||
if ("text/plain".equals(type)) {
|
||||
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
|
||||
} else if (type.startsWith("image/")) {
|
||||
launchWallpaperActivity(intent.getParcelableExtra(Intent.EXTRA_STREAM), type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String generateRandomString() {
|
||||
int leftLimit = 97; // letter 'a'
|
||||
int rightLimit = 122; // letter 'z'
|
||||
int targetStringLength = 3;
|
||||
Random random = new Random();
|
||||
StringBuilder buffer = new StringBuilder(targetStringLength);
|
||||
for (int i = 0; i < targetStringLength; i++) {
|
||||
int randomLimitedInt = leftLimit + (int)
|
||||
(random.nextFloat() * (rightLimit - leftLimit + 1));
|
||||
buffer.append((char) randomLimitedInt);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private void handleUrl(String textUrl) {
|
||||
URL url;
|
||||
|
||||
try {
|
||||
url = new URL(textUrl);
|
||||
} catch (MalformedURLException e) {
|
||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
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());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void launchWallpaperActivity(Uri uri, String mimeType) {
|
||||
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
4
app/src/main/res/xml/file_paths.xml
Normal file
4
app/src/main/res/xml/file_paths.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<paths>
|
||||
<cache-path name="cache" path="." />
|
||||
</paths>
|
Loading…
Reference in New Issue
Block a user