Search code examples
javaandroidmultithreadingrunnablepicasso

How can I pass a Variable to this run() function?


The class I am talking about here comes from the Picasso Lib for Android. I have a variable inside this function called "ImageName". However I cant seem to figure out how to pass a parameter to the function to change that variables name. I have tried extending the Target class but I still seem to get errors and I am not advanced enough to know what I am doing wrong.

private Target target = new Target() {
    @Override
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from)
{
        new Thread(new Runnable() {
            @Override
            public void run() {

                File file = new File(
                        Environment.getExternalStorageDirectory().getPath()
                                + ImageName + ."jpg");//+ "/saved.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                    ostream.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

//WHAT I HAVE COME UP WITH in the mean time...

public interface TargetImageName extends Target
{

    public void TargetImageName(String ImageName);

    @Override
    void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from);

    @Override
    void onBitmapFailed(Drawable errorDrawable);

    @Override
    void onPrepareLoad(Drawable placeHolderDrawable);
}

private TargetImageName target2 = new TargetImageName() {
    String name;
    @Override
    public void TargetImageName(String ImageName) {
        name = ImageName;
    }

    @Override
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                File file = new File(
                        Environment.getExternalStorageDirectory().getPath()
                                + name);//+ "/saved.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
                    ostream.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

Solution

  • You cannot pass anything to run() as it takes no arguments. What you should do is simply extend Runnable and pass whatever you need via constructor:

    public class MyRunnable implements Runnable {
         protected Integer mFoo;
    
         public MyRunnable(Integer foo) { 
            mFoo = foo;
         }
    
         @Override
         public void run() {
              // access mFoo when you need it
               ...
         }
    }
    

    and then

    ...
     new Thread(new MyRunnable(...)) {
        ...