WikiStyle
WikiStyle

Reputation: 21

Why I can't display the image on Android platform

On the site http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9, we can get a gif image named ibikegif.gif. However I can't display it on my android phone. My code like this:

/**
    *@param url the imageURL.it references to 
    *"http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9" here
    *
    **/
    public Bitmap returnBitMap(String url) {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is); //Attention:bitmap is null     
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

Could you help me?

Upvotes: 1

Views: 239

Answers (1)

ibecar
ibecar

Reputation: 415

I have tested this code and it works. It's basically what you did with one difference, i have also set request method explicitly to GET.

public class MyActivity extends Activity {

    private ImageView imageView;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new AsyncTask<Void, Void, Void>() {
            Bitmap bitmap;

            @Override
            protected Void doInBackground(Void... params) {
                try{
                    bitmap = returnBitMap("http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                imageView.setImageBitmap(bitmap);
                imageView.invalidate();
            }

        }.execute();
    }

    public Bitmap returnBitMap(String url) throws IOException {
        URL myFileUrl = null;
        Bitmap bitmap = null;
        InputStream is = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.setRequestMethod("GET");
            conn.connect();
            is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(is != null) {
            is.close();
        }
        return bitmap;
    }
}

Upvotes: 1

Related Questions