1

I have been trying to make a wallpaper app. When we click on a specific image from the gallery, it opens a new activity as below, where the image is displayed in full size and there is a button to set it as the wallpaper. I am using Glide library to load the pictures from the url. Please tell me how to set it as the wallpaper from the url as thee app loads the full size picture but crashes every time I click on the button. Maybe it is a problem with ether loading from the url or with setting it as the wallpaper.

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;
    }
  }
}

I have set the permission in AndroidManifest as

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />

This is the error that I get on pressing the 'set as wallpaper' button:-

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)

Please also if someone could suggest if it is a problem with the permission as I am using a marshmallow device.

Thanks a lot, in advance.

Abhriya Roy
  • 1,338
  • 17
  • 23

1 Answers1

1

I found when you click the button, you do some http connection in the getBitmapFromURL method. Maybe this is why you got an exception because it is not allowed to do any network on main thread or you will get android.os.NetworkOnMainThreadException.

Try to use a new thread to put your getBitmapFromURL method in.


After checking your log, It is really an NetworkOnMainThreadException.

You can set the 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();
    }
});
L. Swifter
  • 3,179
  • 28
  • 52