-2
   Bitmap bitmap = BitmapFactory.decodeStream(result);

HERE IN LOG I AM GETTING

   03-05 06:47:36.639: E/bitmap(931): android.graphics.Bitmap@417d5948

BUT THEN EXCEPTION COMES OF NULL POINTER EXCEPTION

  public class DetailsActivity extends Activity {
private ImageView image;
 //ASYNCTASK

class DownloadImageTask extends AsyncTask<Void, Void, InputStream> {

    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(DetailsActivity.this);
        dialog.setTitle("Please wait");
        dialog.setMessage("Please wait while the application is downloading the image");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected InputStream doInBackground(Void...  params) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        url FROM WHERE WE HAVE TO FETCH IMAGE
        String stringURL = "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png";
        try {
            Log.e("URL TEST",""+stringURL);
            stringURL=stringURL.replaceAll(" ", "%20");
            URL url = new URL(stringURL);
            Log.e("URL TEST",""+url);
            //stringURL=stringURL.replaceAll(" ", "%20");
            URLConnection connection = url.openConnection();
            InputStream stream = connection.getInputStream();
            Log.e("TESTING","TESTING"+ stream);
            return stream;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(InputStream result) {
        super.onPostExecute(result);
        //Bitmap bi = BitmapFactory.
        try{Bitmap bitmap = BitmapFactory.decodeStream(result);
        Log.e("bitmap",""+bitmap);
          ERROR AT THIS LINE==>image.setImageBitmap(bitmap);


        //Log.e("final"," " + image.setImageBitmap(bitmap));
        }catch(Exception e){
            Log.e(" YNull" , ""+ e);//NULL POINTER EXCEPTION
        }

        dialog.cancel();
    }

}

private void asyncDownload() {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectNetwork().build());

    DownloadImageTask task = new DownloadImageTask();
    task.execute();
}

///////////////////////////////////////////////////////////////////////////////
Async Dowloader
/////////////////////////////////////////////////////////////////////////////
ONCREATE METHORD

IMAGE IS NOT GETTING FETCHED

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    image = (ImageView)findViewById(R.id.imageView2);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    setContentView(R.layout.activity_details);
    asyncDownload();
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
tinos07
  • 105
  • 1
  • 11

2 Answers2

1

First correct this You should return InputStream stream

return stream;

Instead of

return null;

in doInBackground() in your DownloadImageTask

M D
  • 47,665
  • 9
  • 93
  • 114
  • :: see the code carefully i have returned the stream only – tinos07 Mar 05 '14 at 12:36
  • @tinos07 no no you see carefully plz – M D Mar 05 '14 at 12:38
  • URLConnection connection = url.openConnection(); InputStream stream = connection.getInputStream(); Log.e("TESTING","TESTING"+ stream); return stream; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } – tinos07 Mar 05 '14 at 15:18
0

Don't return the input stream. Download the complete data into a byte array and return it. Then in onPostExecute, use the byte array to decode the image.

@Override
protected byte[] doInBackground(Void...  params) {
....
    Log.e("URL TEST",""+stringURL);
    stringURL=stringURL.replaceAll(" ", "%20");
    URL url = new URL(stringURL);
    Log.e("URL TEST",""+url);
    //stringURL=stringURL.replaceAll(" ", "%20");
    URLConnection connection = url.openConnection();

    DataInputStream stream = new DataInputStream(connection.getInputStream());
    int len = connection.getContentLength();
    byte[] data = new byte[len];
    stream.readFully(data, 0, len);
    Log.e("TESTING","TESTING"+ stream);
    return data;
....
}

Then in onPostExecute, use this:

@Override
protected void onPostExecute(byte[] result) {
    super.onPostExecute(result);
    //Bitmap bi = BitmapFactory.
    try{
        Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
        Log.e("bitmap",""+bitmap);
        //Log.e("final"," " + image.setImageBitmap(bitmap));
    } catch(Exception e){
        Log.e(" YNull" , ""+ e);//NULL POINTER EXCEPTION
    }

    dialog.cancel();
}
amuttsch
  • 1,254
  • 14
  • 24