BitmapFactory返回null,尽管存在一张图片。

5

我想将一个字符串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 是指什么?它是有效的图像文件吗?你尝试查看它的二进制内容了吗? - pskink
imgPath是一个有效的URL。 - Swanand
我的URL类似于www.facebook.com/abc/sampleImage.png - Swanand
decodeFile(String pathName)自 API level 1 开始添加 将文件路径解码为位图。如果指定的文件名为空,或无法解码为位图,则该函数返回 null。 - pskink
我已经交叉检查过了。URL 包含值,不是空的。 - Swanand
你知道 文件路径 是什么意思吗?请再次阅读 decodeFile 的文档。 - pskink
3个回答

3

Ravindra的回答很有帮助,为了更好地利用图片,请尝试使用Picasso库,它非常出色。它还具有调整大小/裁剪的方法。

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
您可以使用此参考资料,它可能对您有所帮助。
注意:该函数会进行网络连接,您应该在线程或AsyncTask中调用它。否则它可能会抛出NetworkOnMainThread异常。
由于该函数返回位图,因此您必须等待线程执行完成,因此请查看使用join()的问题。
我希望这能帮到您。

1
太棒了,我很高兴能帮到你。 - Nakul

1
使用以下代码行:-
    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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接