Search code examples
androidimagebitmapfactory

BitmapFactory.decodeFile(String imagePath) returns null even image exists


I don't know , why I am getting null from BitmapFactory.decodeFile(String imagePath) method. imagePath is perfect.Code is below here .

public static byte[] imageToByteArray(String imagePath){
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG , 100 , byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

imaagePath is a internet specific path .Here I am using google place api and imagePath is location of image given by google web service.


Solution

  • decodeFile is use to get Bitmap from local File system.

    Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.
    

    To get Bitmap from internet use

    Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
    

    Do not forget to run above line in background thread.