-1

I am making a wallpaper app, I want to set image from url as wallpaper. I assigned url by using

url = intent.getStringExtra("tmp");

from previous activity. I called this code in onClick method.

  Bitmap result ;
  WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
  try {
            result= Picasso.with(this)
                    .load(url)
                    .get();
            wallpaperManager.setBitmap(result);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

I am using picasso library to set image as wallpaper but I have a problem that I couldn't understand why this error occurs.

Not: ImageLoader is another class to display images.

Activity:

package gc.x;

import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.io.IOException;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    ImageLoader imageLoader;
    ImageView image;
    String url;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = this.getIntent();
      //  intent.getStringExtra("tmp");

        image = (ImageView)findViewById(R.id.image4) ;

        Button button = (Button) findViewById(R.id.button1);


        imageLoader = new ImageLoader(this.getApplicationContext());

        url = intent.getStringExtra("tmp");
        imageLoader.DisplayImage(url, image);


        if(button != null)
        button.setOnClickListener(SecondActivity.this);






    }


    @Override
    public void onClick(View v) {

        Bitmap result ;
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        try {
            result= Picasso.with(this)
                    .load(url)
                    .get();
            wallpaperManager.setBitmap(result);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }



}

EDIT: I tried a solution but it is not working. It does not give error, but nothing happens when I click the button. My New Code:

package gc.x;

import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.io.IOException;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    ImageLoader imageLoader;
    ImageView image;
    String url;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = this.getIntent();
      //  intent.getStringExtra("tmp");

        image = (ImageView)findViewById(R.id.image4) ;

        Button button = (Button) findViewById(R.id.button1);


        imageLoader = new ImageLoader(this.getApplicationContext());

        url = intent.getStringExtra("tmp");
        imageLoader.DisplayImage(url, image);


        if(button != null)
        button.setOnClickListener(SecondActivity.this);

    }


    @Override
    public void onClick(View v) {
        new SetWallpaperTask();
        Toast.makeText(getApplicationContext(), "Set wallpaper successfully", Toast.LENGTH_SHORT).show();

    }

    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);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void onPreExecute () {
            super.onPreExecute();

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }



}
serkan stack
  • 155
  • 5
  • 15

2 Answers2

1

I defined a method that returns bitmap from url

   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) {
            // Log exception
            return null;
        }
    }

Then a new thread in OnClick method to prevent android.os.NetworkOnMainThreadException error.

Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try  {
                            WallpaperManager myWallpaperManager
                                    = WallpaperManager.getInstance(getApplicationContext());
                            Bitmap bitmap= getBitmapFromURL(url);
                            if(bitmap!=null)
                                myWallpaperManager.setBitmap(bitmap);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                thread.start();

Final Activity:

package gc.x;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class SecondActivity extends Activity {

    ImageLoader imageLoader;
    ImageView image;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = this.getIntent();
      //  intent.getStringExtra("tmp");

        image = (ImageView)findViewById(R.id.image4) ;

        Button button = (Button) findViewById(R.id.button1);


        imageLoader = new ImageLoader(this.getApplicationContext());

        url = intent.getStringExtra("tmp");
        imageLoader.DisplayImage(url, image);

        if(button != null)
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try  {
                            WallpaperManager myWallpaperManager
                                    = WallpaperManager.getInstance(getApplicationContext());
                            Bitmap bitmap= getBitmapFromURL(url);
                            if(bitmap!=null)
                                myWallpaperManager.setBitmap(bitmap);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                thread.start();

            }
        });

    }

    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) {
            // Log exception
            return null;
        }
    }


}
serkan stack
  • 155
  • 5
  • 15
0

EDIT: original question was a simple handling of an exception. Once handled it revealed further problems in the code which the OP has changed his question to reflect. Without mention of his original question.

Placing

        Bitmap result= Picasso.with(this) //XX Problem Occurs Here
            .load("url") 
            .get();

Inside the IOException try/catch should fix this.

Imagine there is nothing to load().get() you would have an IOException. Since the possibility exists you need to handle it in case it happens.

    @Override
public void onClick(View v) {

    Bitmap result;
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);

    try {
        result = Picasso.with(this).load("url").get();
        wallpaperManager.setBitmap(result);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Doing this should handle the exception.

Shannon
  • 884
  • 8
  • 15
  • Are you sure? I think it is not a solution. – serkan stack Oct 18 '16 at 21:48
  • Yes I tried. When I place in IOException it gives error for result bitmap firstly because it is assigned before declaration. – serkan stack Oct 18 '16 at 21:57
  • Does it fix the your IOException? It's not clear what is happening sorry. Is the Bitmap result above "wallpaperManager.setBitmap(result);" in the try catch? – Shannon Oct 18 '16 at 23:04
  • I tried this edited solution it solves IOException problem but new "java.lang.IllegalStateException: Method call should not happen from the main thread. " occurs. – serkan stack Oct 18 '16 at 23:23
  • This http://stackoverflow.com/questions/27180765/picasso-java-lang-illegalstateexception-method-call-should-not-happen-from-the may be of help. – Shannon Oct 18 '16 at 23:37
  • Thanks for help, I solved my problem. I will share my solution now. – serkan stack Oct 19 '16 at 11:22
  • I solved the original question you had. I see you changed your entire question to another existing problem my solution revealed. – Shannon Oct 22 '16 at 08:36