【问题标题】:Drawable Height and Width is Not Working in Android ICS可绘制的高度和宽度在 Android ICS 中不起作用
【发布时间】:2013-02-05 11:39:35
【问题描述】:

我正在尝试在 android 中获取图像宽度和高度并相应地加载购买我下面的代码在 2.3.3 中工作正常,但是当我在更高版本(4.0)中测试时,它的高度和宽度给出空指针异常.. .下面是我的代码...

           Drawable drawable = null; 
           drawable = LoadImageFromWebOperations(logo_url.replace(" ", "%20"));
           System.out.println("Width" + drawable.getMinimumWidth());
           System.out.println("Height" + drawable.getMinimumHeight()); 
           int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, drawable.getMinimumWidth(), mCtx.getResources().getDisplayMetrics());
           int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, drawable.getMinimumHeight(), mCtx.getResources().getDisplayMetrics());
           client_logo.setMinimumHeight(height);
           client_logo.setMinimumWidth(width);
           client_logo.setImageDrawable(drawable); 


            public Drawable LoadImageFromWebOperations(String url){
System.out.println(url);

    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return (d);
    }catch (Exception e) {
        //System.out.println("Exc="+e);
        return null;
    }
}

【问题讨论】:

    标签: android image drawable image-size


    【解决方案1】:

    从 API 11 开始,如果您在 UI 线程上执行此代码,网络访问将抛出 NetworkOnMainThreadException。您没有发布您的 logcat 输出(这将非常有帮助),但我怀疑这是正在发生的事情。因此,LoadImageFromWebOperations 将返回 null,当您尝试访问可绘制属性时,您将获得 NPE。

    您需要在工作线程中运行此代码。见文章Keeping Your App Responsive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2013-10-26
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2018-03-03
      • 2020-10-18
      相关资源
      最近更新 更多