BitmapFactory returns null 尽管存在图像

BitmapFactory returns null though there exist an image

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

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;

}

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

你可以使用这个参考,对你来说可能是helpfull

注意:- 此函数创建网络连接,您应该在线程或 AsyncTask 中调用它。否则它可能会抛出 NetworkOnMainThread 异常。

由于函数正在返回位图,您将不得不等待线程执行完毕,因此请检查此 question。 它使用 join()

希望对您有所帮助。

为此使用这些代码行:-

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

Ravindra 的 回答很有帮助,为了更好地利用图像,请尝试 Picasso lib,令人兴奋。它也有 resize/crop 方法。

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) {

                    }
                });