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

会有一股神秘感。 提交于 2019-12-11 13:23:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13767199/bitmapfactory-decodefilestring-imagepath-returns-null-even-image-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!