如何下载BMP图像并在ImageViewer中显示它

时间:2014-03-20 15:30:06

标签: android bitmap android-asynctask connection imageview

我正在尝试在我的Android应用程序中下载并显示BMP图片,问题是当我运行应用程序时,根本没有发生任何变化...如果你能给我任何积分,那就太棒了! :)

堆栈追踪:http://laravel.io/bin/wOyQ

更新:新代码

public String imageUrl = "http://rory-price.co.uk/uni/images/444444.bmp";

private class LoadTask extends AsyncTask<String,Integer, Bitmap>
{
    @Override
    protected Bitmap doInBackground(String... params)
    {
        try
        {
            URL urlstring = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) urlstring.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        }

        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap myBitmap) {
        ImageView iv = (ImageView) findViewById(R.id.imageView);
        iv.setImageBitmap(myBitmap);
    }
    @Override
    protected void onPreExecute() {}
}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

         public String imageUrl = "http://rory-price.co.uk/uni/images/444444.bmp";

 private class LoadTask extends AsyncTask<String,Integer, Bitmap>
 {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;

    @Override
    protected Bitmap doInBackground(String... params)
    {
      try
      {
        URL urlstring = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) urlstring.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input, null, bmOptions);

        return myBitmap;
    }

    catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }
}

    @Override
    protected void onPostExecute(Bitmap myBitmap) {
        ImageView iv = (ImageView) findViewById(R.id.imageView);
        iv.setImageBitmap(myBitmap);
    }

    @Override
    protected void onPreExecute() {}
}
}

确保清单文件中有<uses-permission android:name="android.permission.INTERNET" />