3

我是 java 和 android 开发的新手。我试图找到这个问题的答案,但似乎很明显,所以没有人问这个问题。我用这个例子来显示一个图像:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String str="http://logproj.500mb.net/image.php?id=8";
    ImageView imView;
    imView = (ImageView)findViewById(R.id.image);
     try{
    url = new URL(str);
    }
    catch(MalformedURLException e)
    {
            e.printStackTrace();
    }
    try{
            HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
            conn.setDoInput(true);

            conn.connect();
            int length = conn.getContentLength();

            int[] bitmapData =new int[length];
            byte[] bitmapData2 =new byte[length];

            InputStream is = conn.getInputStream();

            bmp = BitmapFactory.decodeStream(is);
            imView.setImageBitmap(bmp);
            } catch (IOException e)
            {
                    e.printStackTrace();
            }

}

它适用于 jpg 图像,但我的图像是 bmp 并且应用程序崩溃或“意外停止”。

我希望你能帮助解决这个问题。提前致谢。

4

4 回答 4

2

尝试使用 apaches Web 客户端。这应该有效。让我知道。

public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
{
    HttpResponse response=null;
    Bitmap b=null;
    InputStream instream=null;

    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    decodeOptions.inPreferredConfig = bitmapCOnfig;
    try
    {
    HttpGet request = new HttpGet(url.toURI());
        response = client.execute(request);
        if (response.getStatusLine().getStatusCode() != 200)
        {
            Log.d("Bad response on " + url.toString());
            Log.d("http response: " + response.getStatusLine().toString());
            return null;
        }
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
        instream = bufHttpEntity.getContent();

        return BitmapFactory.decodeStream(instream, null, decodeOptions);
    }
    catch (Exception ex)
    {
        Log.d("error decoding bitmap from:" + url, ex);
        if (response != null)
        {
            Log.d("http status: " + response.getStatusLine().getStatusCode());
        }
        return null;
    }
    finally
    {
        if (instream != null)
        {
            try {
                instream.close();
            } catch (IOException e) {
                Log.d("error closing stream", e);
            }
        }
    }
}

不要忘记从异步任务内部调用此函数。

于 2012-12-09T22:46:03.180 回答
2

首先 - 您不应该在主 UI 线程上运行 http 连接。在较新版本的 android 上,这将引发导致强制关闭的 networkOnUIThreadException。

我建议编写一个AsyncTask在后台线程上运行您的下载。WIIJBD 编写的 decodeFromUrl 看起来应该可以正常工作,因此如果您调用该函数,您可以让它将位图返回给 UI 线程 onPostExecute() 并在 ImageView 中设置

异步任务教程:http ://www.vogella.com/articles/AndroidPerformance/article.html

有更多问题请告诉我

于 2012-12-09T23:52:38.720 回答
0

不要使用 .bmp 文件。请改用 .png 文件。或者 .gif - 这在 android 上不太可取,但确实有效。

于 2012-12-09T23:45:18.583 回答
0

我在一些带有图像的android应用程序上工作过,如果图像很大,应用程序崩溃了很多,所以我用谷歌搜索并在android开发者网络上找到了这个,非常好。

http://developer.android.com/training/displaying-bitmaps/index.html

我要向您推荐的是,您可以下载图像并将其保存到外部存储设备(如 SD 卡),然后加载并显示,这一次您可以根据内存使用情况降低其质量。

另外下载图像在您的应用程序中是耗时的部分,因此最好将这些代码放到另一个线程或AsyncTask(更好,IMO),这样在从互联网下载图像时它不会停止 UI 主线程,完成后您可以显示它也是。

希望能帮助到你。

于 2012-12-09T23:54:48.243 回答