- Set Bild als wallpaper aus der url
Ich möchte eine app erstellen, die dem Benutzer ermöglicht, ein Bild als wallpaper durch klicken auf eine Schaltfläche. Dieses Bild würde sich in einem url und der Einstellung der Tapete erfolgt über AsyncTask. Ich habe die Schritte wie in diesem video gezeigt: https://www.youtube.com/watch?v=JeA8Z8dtD10 aber es funktioniert nicht für mich. Die app zeigt den Knopf, aber wenn ich es auf alles, was passiert.
Hier ist der code:
package com.example.myapplication4;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends Activity {
public ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSetWallpaper = (Button) findViewById(R.id.button);
btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String urlImage ="https://www.geektopia.es/storage/geek/posts/2015/08/17/marshmallow.jpg";
new SetWallpaperTask().equals(urlImage);
}
});
}
public InputStream OpenHttpConnection (String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL (urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpsURLConnection)) {
throw new IOException("Not an HTTP connection");
}
try {
HttpsURLConnection httpCon = (HttpsURLConnection)conn;
httpCon.setInstanceFollowRedirects(true);
httpCon.setRequestMethod("Get");
httpCon.connect();
response = httpCon.getResponseCode();
if (response == HttpsURLConnection.HTTP_OK) {
in = httpCon.getInputStream();
}
}catch (Exception ex) {
throw new IOException("Error connecting...");
}
return in;
}
public Bitmap DecodeStream (String url) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(url);
bitmap = BitmapFactory.decodeStream(in);
in.close();
}
catch (IOException e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
return bitmap;
}
public class SetWallpaperTask extends AsyncTask <String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = DecodeStream(params[0]);
return bitmap;
}
@Override
protected void onPostExecute (Bitmap result) {
super.onPostExecute(result);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
try {
wallpaperManager.setBitmap(result);
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Set wallpaper successfully", Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
protected void onPreExecute () {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
EDIT:
public class MainActivity extends Activity {
public ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSetWallpaper = (Button) findViewById(R.id.button);
btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SetWallpaperTask();
}
});
}
public class SetWallpaperTask extends AsyncTask <String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
Bitmap result= null;
try {
result = Picasso.with(getApplicationContext())
.load("https://www.geektopia.es/storage/geek/posts/2015/08/17/marshmallow.jpg")
.get();
} catch (IOException e) {
e.printStackTrace();
}
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(result);
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute (Bitmap result) {
super.onPostExecute(result);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
try {
wallpaperManager.setBitmap(result);
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Set wallpaper successfully", Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
protected void onPreExecute () {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
Außerdem habe ich INTERNET und SET_WALLPAPER-Berechtigungen, die das Manifest. Weißt du wo der Fehler ist? Vielen Dank 🙂
- neue SetWallpaperTask(); oder neue SetWallpaperTask().execute()
Du musst angemeldet sein, um einen Kommentar abzugeben.
Anstatt zu versuchen, laden Sie das Bild selbst und dann zu verarbeiten. Stattdessen ein image loading library wie Picasso. Mit Picasso alles, was Sie brauchen, um in Ihren Klick-listener ist:
Schön und einfach, ohne sich mit threading.
Sie brauchen Picasso Bibliothek bitmap aus url
Hinzufügen Neueste Bibliothek https://github.com/square/picasso