BitmapFactory.decodeFile(String imagePath) 返回 null 即使图像存在


我不知道

,为什么我从BitmapFactory.decodeFile(String imagePath)方法得到null。imagePath是完美的。代码在下面 这里 .

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是一个互联网特定的路径。在这里,我正在使用谷歌地方api,imagePath是谷歌网络服务给出的图像位置。

decodeFile 用于从本地文件系统获取Bitmap

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.

从互联网使用中获得Bitmap

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

不要忘记在后台线程中运行上述行。

最新更新