[英]java.lang.OutOfMemoryError: Android
I am facing issue with load bitmap image in one of my activity with cropper library.我在使用cropper库的一项活动中遇到加载位图图像的问题。 I am getting outofmemory error if image size is more than 5 MB...I have tried如果图像大小超过 5 MB,我会出现内存不足错误......我试过了
android:largeHeap="true"
but it have not helped me for solve issue.但它没有帮助我解决问题。 My java code and logcat is like below我的java代码和logcat如下
Logcat:日志猫:
07-09 21:10:47.482: E/art(4870): Throwing OutOfMemoryError "Failed to allocate a 6391674 byte allocation with 6004224 free bytes and 5MB until OOM" 07-09 21:10:47.482: D/AndroidRuntime(4870): Shutting down VM 07-09 21:10:47.522: E/AndroidRuntime(4870): FATAL EXCEPTION: main 07-09 21:10:47.522: E/AndroidRuntime(4870): Process: com.karopass.status2017, PID: 4870 07-09 21:10:47.522: E/AndroidRuntime(4870): java.lang.OutOfMemoryError: Failed to allocate a 6391674 byte allocation with 6004224 free bytes and 5MB until OOM 07-09 21:10:47.522: E/AndroidRuntime(4870): at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122) 07-09 21:10:47.522: E/AndroidRuntime(4870): at com.karopass.status2017.material.ImageLoader.addToDiskCache(ImageLoader.java:238) 07-09 21:10:47.522: E/AndroidRuntime(4870): at com.karopass.status2017.material.ImageLoader.saveTempImage(ImageLoader.java:271) 07-09 21:10:47.482:E/art(4870):抛出 OutOfMemoryError“无法分配 6391674 字节的分配,其中 6004224 个可用字节和 5MB 直到 OOM” 07-09 21:10:47.482(D/7AndroidRuntime) : 关闭 VM 07-09 21:10:47.522: E/AndroidRuntime(4870): FATAL EXCEPTION: main 07-09 21:10:47.522: E/AndroidRuntime(4870): Process: com.karopass.status2017, PID 4870 07-09 21:10:47.522: E/AndroidRuntime(4870): java.lang.OutOfMemoryError: 无法分配 6391674 字节分配,6004224 可用字节和 5MB 直到 OOM 07-09 21:1022:47/Run (4870): 在 java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122) 07-09 21:10:47.522: E/AndroidRuntime(4870): 在 com.karopass.status2017.material.ImageLoader.addToDiskCache(ImageLoader. java:238) 07-09 21:10:47.522: E/AndroidRuntime(4870): 在 com.karopass.status2017.material.ImageLoader.saveTempImage(ImageLoader.java:271)
as well my java code is like below以及我的java代码如下
public File addToDiskCache(String imageName, Bitmap inImage) {
Log.e("Add to Disk",imageName);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
if(!mCacheDir.exists()){
mCacheDir.mkdirs();
}
File filePath=new File(mCacheDir.getPath()+File.separator+imageName);
if(!filePath.exists()) {
try {
filePath.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os=null;
try {
os= new FileOutputStream(filePath.getPath());
os.write(bytes.toByteArray());
os.flush();
} catch (IOException e) {
Log.e("Write err",filePath.toString());
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e("storage path",filePath.toString());
return filePath;
}
Please help me for solve issue.请帮我解决问题。
Thanks谢谢
compress is ineffective压缩无效
try this when you use bitmap from file当你使用文件中的位图时试试这个
private Bitmap sampleImage(Bitmap bitmap, int reqWidth, int reqHight) {
//first decode
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile("FILE_PATH",options);
//seconde deconde
options.inSampleSize = caculateSampleSize(options,reqWidth,reqHight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile("FILE_PATH",options);
return bitmap;
}
private int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHight) {
int screenWidth;
int screenHight;
screenWidth = (reqWidth == 0 ? this.getResources().getDisplayMetrics().widthPixels : reqWidth);
screenHight = (reqHight == 0 ? this.getResources().getDisplayMetrics().heightPixels : reqHight);
int sampleWith = options.outWidth / screenWidth;
int sampleHight = options.outHeight / screenHight;
//use max
return sampleWith > sampleHight ? sampleWith : sampleHight;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.