Tôi đang xây dựng một ứng dụng xử lý hình ảnh và cũng đã trả về null khi thử các phương pháp này. Bảng điều khiển của tôi trả về không phải là một lỗi mà là một câu lệnh degub như thế này
D / skia: --- decoder-> decode trả về false
Đây là những gì tôi tìm thấy sẽ tải bất kỳ bitmap nào trong ứng dụng của tôi
1) tên tệp chính xác với các quyền thích hợp
2) Thu nhỏ hình ảnh khi thích hợp
3) Nếu bạn cần một hình ảnh lớn, hãy đặt nó bên dưới bạn biểu hiện như vậy
<application
android:largeHeap="true"
</application>
Sử dụng một heap lớn không thể thay thế cho việc hiển thị kích thước hình ảnh chính xác. Đây là ví dụ về mã tôi sử dụng trực tiếp từ tài liệu android
public Bitmap getRawImageThumb(Context mContext)
{
Bitmap b = null;
int reqHeight = 100, reqWidth = 100;
String filename = mContext.getFilesDir().toString() + "/" + rawFileName;
Log.d(TAG, "processRawReceipt: " + rawFileName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(filename, options);
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Log.d(TAG, "getRawImage: " + String.valueOf(height) + " " + String.valueOf(width) + " " + String.valueOf(inSampleSize));
b = BitmapFactory.decodeFile(filename, options);
return b;
}