Android BitmapFactory.decodeFile返回null-写入或读取文件时出现问题?
这是我将位图保存到SD卡的代码:Android BitmapFactory.decodeFile返回null-写入或读取文件时出现问题?,android,bitmap,Android,Bitmap,这是我将位图保存到SD卡的代码: public static boolean savePhoto(String fileName, Bitmap photo) { deleteOldPhotos(); File sdCardPath = new File(MyApplication.getSDCardPathForPhotos()); if (!sdCardPath.exists()) sdCardPath.mkdirs(); File desti
public static boolean savePhoto(String fileName, Bitmap photo) {
deleteOldPhotos();
File sdCardPath = new File(MyApplication.getSDCardPathForPhotos());
if (!sdCardPath.exists())
sdCardPath.mkdirs();
File destination = new File(sdCardPath, fileName);
try {
FileOutputStream out = new FileOutputStream(destination);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
用于从SD卡中检索并转换为Base64字符串:
public static String getEncodedPhoto(String filename) {
File photoPath = new File(MyApplication.getSDCardPathForPhotos(), filename);
if (photoPath.exists()) {
Bitmap photo = BitmapFactory.decodeFile(photoPath.getAbsolutePath());
setBugSenseExtraCrashData(filename, photoPath, photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] photoByteArray = stream.toByteArray();
String photoBase64 = Base64.encodeToString(photoByteArray, Base64.DEFAULT);
return photoBase64;
} else
return null;
}
我已经搜索了整个stackoverflow,并找到了与此问题相关的答案,问题是所问的问题涉及一个静态图像/文件。在我的例子中,系统正在生产中,有时我会收到错误提示,告诉我photo
对象为null,并在photo.compress(Bitmap.CompressFormat.JPEG,100,stream)中抛出NullPointerException代码>行。文件存在,因为它通过了photoPath.exists()
我会做错什么?问题出在save方法还是load方法?有什么帮助或建议吗?尝试使用png而不是jpeg,看看是否有什么不同。我也遇到过同样的问题,用png代替jpeg修复了这个问题。我会试试的!你认为我可以得到这些照片,不能通过其他方法解码(我的意思是发送到我的服务器)?我不希望客户丢失这些照片。提前谢谢。