我一直在尝试制作壁纸应用程序。当我们单击图库中的特定图像时,它会打开一个新的活动,如下所示,其中图像以全尺寸显示,并且有一个按钮将其设置为壁纸。我正在使用 Glide 库从 url 加载图片。请告诉我如何将其设置为 url 中的壁纸,因为您的应用程序会加载完整尺寸的图片,但每次单击按钮时都会崩溃。也许这是从 url 加载以太或将其设置为墙纸的问题。
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main2Activity extends AppCompatActivity {
ImageView img;
private Bitmap theBitmap;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context c= getApplicationContext();
setContentView(R.layout.activity_main2);
b= (Button) findViewById(R.id.button);
Intent i=getIntent();
final String url=i.getStringExtra("picture");
img= (ImageView) findViewById(R.id.imageView4);
Glide.with(this).load(url).fitCenter() .into(img);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
theBitmap=getBitmapFromURL(url);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
try {
wallpaperManager.setBitmap(theBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
我已将 AndroidManifest 中的权限设置为
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
这是我按下“设置为墙纸”按钮时出现的错误:-
07-20 09:33:43.127 3024-3024/com.android.backgroundchooser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.backgroundchooser, PID: 3024
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:755)
at com.android.okhttp.okio.Okio$1.write(Okio.java:76)
at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:902)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:788)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at com.android.backgroundchooser.Main2Activity.getBitmapFromURL(Main2Activity.java:60)
at com.android.backgroundchooser.Main2Activity$1.onClick(Main2Activity.java:42)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21156)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
如果有人可以建议在我使用棉花糖设备时是否存在权限问题,请也请。
非常感谢,提前。