2012-12-09 134 views
3

我是java和android开发的新手。我试图找到这个问题的答案,但似乎这样没人问的是,是显而易见的.. 我用这个例子来显示图像:如何在android中显示bmp图像

/** 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和应用程序崩溃或“意外中止”。

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

+0

您是否试图动态更改程序中的图像,或者您是否希望修复图像?如果图像已修复,则不需要对其进行编程,只需将其放入res下的drawable-hdpi文件夹中,然后将该图像设置为布局xml文件中的imgveiw即可。 – DrinkJavaCodeJava

+0

所以我尝试了你所建议的一切,但没有任何工作,但最终我找到了解决方案:http://www.androidhive.info/2012/07/android-loading-image-from-url-http/。这真的对我有用。 当我的问题得到很多好的答案时,我感到非常惊喜。在我问其他网站之前(例如俄罗斯的Google Baraza),但是这些网站并没有专注于编程。非常感谢大家! – user1890184

回答

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

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

+1

我完全抱歉 - 我没有看到你会把最后一行放在你的文章中!这是我发布的答案而不是评论:( –

0

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

2

首先 - 你不应该在主UI线程上运行http连接。在较新版本的Android中,这会抛出一个networkOnUIThreadException,导致一个关闭的力量。

我建议你写一个AsyncTask在后台线程上运行你的下载。通过WIIJBD编写的decodeFromUrl看起来像它应该工作得很好,所以如果你们称是函数,你能得到它回报你的位图到UI线程onPostExecute(),并设置在ImageView的有

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

任何更多的问题让我知道

+1

+1)。是的异步任务是伟大的,我爱他们,并尽可能使用它们,如果有关。太多的人不会在他们应该使用的地方使用异步任务,而这仅仅是因为糟糕的编程而导致“iPhone手机比android更平滑”的错误假设的一部分。事实上,任何一个都可以是平滑的或波涛汹涌的,但是android并没有像苹果那样的硬标准和质量控制。 – WIllJBD

0

我已经有图片的一些Android应用程序的工作,如果图像是大型应用程序崩溃了很多,所以我用Google搜索,发现这个一个Android开发者网络上,还不错。

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

我会推荐给你的是,你可以下载并保存图像到外部存储 - 根据您的记忆像SD卡,然后加载它,并显示这个时候你可以减少它的质量用法。

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

希望它有帮助。