我一直在尝试制作壁纸应用。当我们点击图库中的特定图像时,它会打开一个新活动,如下图所示,图像以完整尺寸显示,并有一个按钮将其设置为壁纸。我正在使用Glide库从网址加载图片。请告诉我如何将其设置为网址中的壁纸,因为该应用加载了完整尺寸的图片,但每次点击按钮时都会崩溃。也许从网址加载以太网或将其设置为壁纸是一个问题。
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)
如果我使用棉花糖设备,有人可以建议是否存在许可问题。
非常感谢,提前。
答案 0 :(得分:1)
我发现当您单击该按钮时,您会在getBitmapFromURL
方法中执行一些http连接。也许这就是为什么你有一个例外,因为它不允许在主线程上做任何网络,或者你会得到android.os.NetworkOnMainThreadException
。
尝试使用新线程将getBitmapFromURL
方法放入。
检查完日志后,它确实是NetworkOnMainThreadException
。
您可以设置OnClickListener
:
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap theBitmap=getBitmapFromURL(url);
b.post(new Runnable(){
@Override
public void run() {
// I'm not sure whether these code work out the main thread, so I use a View.post
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
wallpaperManager.setBitmap(theBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}).start();
}
});