0

I have a code which getting a bitmap from url and then changing a imageview but I don't know why this not working.. I trying some similar answers but image still don't set.

This getting a bitmap.

class picture_get extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        URL img_value = null;
        try {
            img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
            Log.i("bitmap_pic_get", "ok");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
        return "Executed";
    }
}

And this setting the imageview

new picture_get().execute();

runOnUiThread(new Runnable() {

@Override
public void run() {
    textView1.setText("Id: "+ id);
    image1.setImageBitmap(bitmap_pic);
}

});

All code working but image still the same.

3
  • Can you be specific about what you're trying to do? Commented Dec 6, 2014 at 17:08
  • 1
    It's hard to say, but it looks to me, from the code you posted, that you are setting the image before it's loaded. Shouldn't you override onPostExecute in AsyncTask, and invoke runOnUIThread(...) from there? Commented Dec 6, 2014 at 17:16
  • I trying to get pic from url and set it on imageview :) Commented Dec 6, 2014 at 17:24

3 Answers 3

2

I would advice implementing an interface in the class where you control the ImageView. and than call it from your onPostExecute(Bitmap bmp) :

public class ImageDownloader
{
public interface ImageDownload
{
    void getImage(Bitmap bmp);
}

Connection conn;
ImageDownload callBack;

public ImageDownloader(ImageDownload cb)
{
    conn = new Connection();
    this.callBack = cb;
}

public AsyncTask<String, Void, Bitmap> downloadFile(ObjectType objectType, int fileID, int width,
                                                    int height, int fitMode)
{
    AsyncTask<String, Void, Bitmap> task = null;
    String url = getURL(objectType,fileID,width,height,fitMode);
    try
    {
            task = new Downloader().executeOnExecutor(
                    AsyncTask.THREAD_POOL_EXECUTOR, url);
    }
    catch (Exception e)
    {
        callBack.getImage(null);
    }

    return task;
}

String getURL(ObjectType objectType, int fileID, int width,
              int height, int fitMode)
{
    String url = conn.URL + FUNCTION
            + "?" + PARAM_OBJECT_TYPE + "=" + objectType.ObjectType()
            + "&" + PARAM_FILE_ID + "=" + fileID
            + "&" + PARAM_WIDTH + "=" + width
            + "&" + PARAM_HEIGHT + "=" + height
            + "&" + PARAM_FIT_MODE + "=" + fitMode;

    return url;
}

private class Downloader extends AsyncTask<String, Void, Bitmap>
{
    @Override
    protected void onPostExecute(Bitmap b)
    {
        super.onPostExecute(b);
        callBack.getImage(b);
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
        //android.os.Debug.waitForDebugger();
        Bitmap bmp = null;
        try
        {
            URL url = new URL(params[0]);

            URLConnection conn = url.openConnection();

            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setDoInput(true);


            InputStream input = conn.getInputStream();
            bmp = BitmapFactory.decodeStream(input);
        } catch (MalformedURLException e) {

        } catch (IOException e) {

        }

        return bmp;
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

1

This should work

class picture_get  extends AsyncTask<String, Bitmap, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        URL img_value = null;
        try {
            img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
            Log.i("bitmap_pic_get", "ok");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
        return bitmap_pic ;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap_pic) {
    image1.setImageBitmap(bitmap_pic);       
    }
}

Comments

0
class picture_get extends AsyncTask<String, Void, Bitmap> {

@Override
protected String doInBackground(String... params) {
    URL img_value = null;
    try {
        img_value = new URL("https://graph.facebook.com/"+id+"picture?type=large");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        bitmap_pic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
        Log.i("bitmap_pic_get", "ok");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //bitmap_pic = Bitmap.createScaledBitmap(bitmap_pic, 50, 50, false);
    return bitmap_pic
}
@Override
void onPostExecute(Bitmap result){

   image1.setImageBitmap(bitmap_pic);
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.