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

时间:2012-12-07 16:35:59

标签: android image bitmapfactory

我不知道,为什么我从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是一个特定于互联网的路径。这里我使用google place api和imagePath是谷歌网络服务提供的图像位置。

1 个答案:

答案 0 :(得分:4)

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

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