Reputation: 67
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.
Upvotes: 0
Views: 3696
Reputation: 1372
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);
}
}
Upvotes: 1
Reputation: 233
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;
}
}
}
Upvotes: 2
Reputation: 2225
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);
}
}
Upvotes: 0