位图错误空指针异常

时间:2014-03-05 12:23:38

标签: android image android-asynctask nullpointerexception bitmapimage

   Bitmap bitmap = BitmapFactory.decodeStream(result);

在这里,我正在获取

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

但是,如果出现异常,则表示出现异常

  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

图像没有被发现

@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();
}

2 个答案:

答案 0 :(得分:1)

首先纠正此问题您应该返回InputStream stream

return stream;

而不是

return null;
<{1}}

doInBackground()

答案 1 :(得分:0)

不要返回输入流。将完整数据下载到字节数组中并返回。然后在onPostExecute中,使用字节数组解码图像。

@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;
....
}

然后在onPostExecute中,使用:

@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();
}