Swanand
Swanand

Reputation: 507

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?

Upvotes: 5

Views: 670

Answers (3)

Nandan Singh
Nandan Singh

Reputation: 1089

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

                    }
                });

Upvotes: 3

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

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

Upvotes: 1

Nakul
Nakul

Reputation: 1329

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.

Upvotes: 2

Related Questions