BitmapFactory returns null, although there is an image

Here I want to convert an image from a String URL. Although there is a URL containing the image, it returns null. I have divided the 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 the if block, the control flow goes to the else block, and BitmapFactory.decodeFile () always returns null. Where am I wrong?

+4
source share
3 answers

You can use this link, it can be helpl for you.

Note. - This function makes a network connection, you must call it inside the stream or AsyncTask. Otherwise, it may cause an NetworkOnMainThreadException.

, , , . join()

, .

+2

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

                    }
                });
+3

Use the following lines of code for this: -

    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);
+1
source

Source: https://habr.com/ru/post/1620271/


All Articles