java - 如何在android中显示bmp图像

标签 java android avd

我是 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 并且应用程序崩溃或“意外停止”。

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

最佳答案

使用 apaches 网络客户端试试这个。这应该工作。让我知道。

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

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

关于java - 如何在android中显示bmp图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792217/

相关文章:

java - 如何将 CSV 文件转换为 List<Map<String,String>>

java - 使用 Java 模拟杂货店排队队列

android - XML 中的 TextInputEditText requestFocus 导致呈现问题

android - AVD 屏幕方向不会随 CTRL+F11 或 CTRL+F12 改变

android - 无法运行 AVD(模拟器)

java - 文件阅读器无法正常工作?

java - 无法使用java处理firefox上selenium webdriver中的警报

android - 在应用程序之间使用隐式 Intent 的自定义操作

php - 从 Android 手机上传图片到笔记本电脑需要太长时间,最后上传时显示“Windows 照片查看器无法显示,因为文件为空”

android - 我无法在Android Studio的AVD Manager中看到