将文件解码为位图有时有效,有时无效
Decoding File to Bitmap sometimes works and sometimes does not
我正在使用EasyImage图库拍照。
然后我将这些文件转换为位图,然后将位图转换为 Base64 并将其上传到服务器。
我知道,这不是一个好方法,但我目前就是这样做的。
拍照时:
@Override
public void onImagePicked(File imageFile, EasyImage.ImageSource source) {
uploadImage(imageFile);
}
这是 "uploadImage" 方法中的第一行:
Image image = new Image(LoginManager.getInstance(getApplicationContext()).getUsername(), file);
这是构造函数:
public Image(String userName, File imageFile) {
this.userName = userName;
this.imageFile = imageFile;
createBase64(getBitmap());
}
内部 "getBitmap" 是问题开始的地方。特别是这两行:
bitmap = BitmapFactory.decodeFile(imageFile.getPath());
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
imageFile 从不 为空。
我用调试器检查了至少 100 次,它永远不会为空。它也总是有路径。
getPath()
永远不会为空。
但是,它仍然经常无法创建位图。
有时会成功,一切正常,但大多数时候,位图为空。
不知道为什么。
文件(拍摄的照片)始终创建成功并且永远不会为空,但由于某种原因它无法创建位图。
Returns
the resulting decoded bitmap, or null if it could not be decoded.
这可能有多种原因,大多数时候位图太大,无法分配 space。
检查图像的路径是否存在,以及您是否对指定的 URI 具有读/写权限。
如果您确实可以访问但仍然失败,您应该将 BitmapFactory.Options
添加到方法调用中并设置 inSampleSize
以加载较小版本的图像。
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
此外,在使用位图时,您应该始终检查返回的 null
,因为内存总是一个问题。
我正在使用EasyImage图库拍照。
然后我将这些文件转换为位图,然后将位图转换为 Base64 并将其上传到服务器。
我知道,这不是一个好方法,但我目前就是这样做的。
拍照时:
@Override
public void onImagePicked(File imageFile, EasyImage.ImageSource source) {
uploadImage(imageFile);
}
这是 "uploadImage" 方法中的第一行:
Image image = new Image(LoginManager.getInstance(getApplicationContext()).getUsername(), file);
这是构造函数:
public Image(String userName, File imageFile) {
this.userName = userName;
this.imageFile = imageFile;
createBase64(getBitmap());
}
内部 "getBitmap" 是问题开始的地方。特别是这两行:
bitmap = BitmapFactory.decodeFile(imageFile.getPath());
bitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
imageFile 从不 为空。
我用调试器检查了至少 100 次,它永远不会为空。它也总是有路径。
getPath()
永远不会为空。
但是,它仍然经常无法创建位图。
有时会成功,一切正常,但大多数时候,位图为空。
不知道为什么。
文件(拍摄的照片)始终创建成功并且永远不会为空,但由于某种原因它无法创建位图。
Returns
the resulting decoded bitmap, or null if it could not be decoded.
这可能有多种原因,大多数时候位图太大,无法分配 space。
检查图像的路径是否存在,以及您是否对指定的 URI 具有读/写权限。
如果您确实可以访问但仍然失败,您应该将 BitmapFactory.Options
添加到方法调用中并设置 inSampleSize
以加载较小版本的图像。
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
此外,在使用位图时,您应该始终检查返回的 null
,因为内存总是一个问题。