Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapFactory returns null though there exist an image

Here I want to convert an image from String URL. Though there is an URL containing image, it returns null. I have shared code below.

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;

}

Instead of executing if block, the control flow enters into else block and BitmapFactory.decodeFile() always returns null. Where I went wrong?

like image 514
Swanand Avatar asked Dec 16 '15 07:12

Swanand


3 Answers

Ravindra's answer is helpful, for better utilization of image try Picasso lib,is mind blowing. It has also resize/crop method.

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

                    }
                });
like image 136
Nandan Kumar Singh Avatar answered Nov 15 '22 00:11

Nandan Kumar Singh


You can use this reference, it might be helpfull to you.

Note :- This function makes Network Connection, you should call it inside thread or AsyncTask. Otherwise it might throw NetworkOnMainThread Exception.

As the function is returning Bitmap, you will have to wait till your thread is executed so check this question. which uses join()

I hope this helps.

like image 23
Nakul Avatar answered Nov 14 '22 23:11

Nakul


Use these lines of code for it:-

    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);
like image 44
Ravindra Kushwaha Avatar answered Nov 14 '22 23:11

Ravindra Kushwaha