【问题标题】:BitmapFactory returns null though there exist an image尽管存在图像,但 BitmapFactory 返回 null
【发布时间】:2016-03-22 05:44:12
【问题描述】:

在这里,我想从字符串 URL 转换图像。虽然有一个包含图像的 URL,但它返回 null。我在下面分享了代码。

private byte[] convertImageToByteArray(String imgPath)
{

    byte[] byteArray = null;
    Bitmap bmp = BitmapFactory.decodeFile(imgPath);
    if(bmp != null)
    {

        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();

            try 
            {
                stream.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    else
    {
        try {
            Bitmap bmpDefault = BitmapFactory.decodeResource(getResources(), R.drawable.na);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //bmpDefault.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bmpDefault.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();

        }

    }
return byteArray;

}

控制流不执行 if 块,而是进入 else 块,BitmapFactory.decodeFile() 总是返回 null。我哪里出错了?

【问题讨论】:

  • imgPath 指的是什么?它是有效的图像文件吗?您是否尝试查看其二进制内容?
  • imgPath 是一个有效的 URL
  • 我的网址就像 www.facebook.com/abc/sampleImage.png
  • decodeFile(String pathName) API 级别 1 中添加 将文件路径解码为位图。如果指定的文件名为空,或者不能解码成位图,函数返回空。
  • 我已经交叉检查过了。 URL 包含值。它不为空。

标签: android bitmapfactory


【解决方案1】:

Ravindra 的 回答很有帮助,为了更好地利用图像尝试 Picasso lib,令人兴奋。它还具有调整大小/裁剪方法。

Picasso.with(getContext()).load("your url").into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        //do what ever you want with your bitmap 
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });

【讨论】:

    【解决方案2】:

    你可以使用这个参考,它可能是helpfull给你。

    注意:- 这个函数建立网络连接,你应该在线程或 AsyncTask 中调用它。否则可能会抛出NetworkOnMainThread Exception。

    当函数返回位图时,你必须等到你的线程被执行,所以检查这个question。 它使用 join()

    我希望这会有所帮助。

    【讨论】:

    • 太棒了,我很高兴,我能帮上忙
    【解决方案3】:

    使用这些代码行:-

        Bitmap bmImg;
       AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>()
    {
        @Override
        protected void onPreExecute()
        {
    
            String _imgURL  = "**HERE IS YOUR URL OF THE IMAGE**";
    
        }
    
        @Override
        protected String doInBackground(String... arg0)
        {
    
    
                HttpGet httpRequest = new HttpGet(URI.create(_imgURL));
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                bmImg = BitmapFactory.decodeStream(bufHttpEntity.getContent());
                System.out.println("main url"+mainUrl);
                httpRequest.abort();
    
    
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
    
    
            return null;
    
        }
    
        @Override
        protected void onPostExecute(String result)
        {
            try
            {
                /////HERE USE YOURS BITMAP
             **bmImg** is your bitmap , you can use it any where i your class
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
    _Task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 2016-03-22
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      相关资源
      最近更新 更多