java.lang.OutOfMemoryError:Android
我在cropper library的一项活动中遇到加载位图图像的问题。如果图像大小超过5MB,我将退出内存错误…我已尝试java.lang.OutOfMemoryError:Android,java,android,bitmap,android-logcat,Java,Android,Bitmap,Android Logcat,我在cropper library的一项活动中遇到加载位图图像的问题。如果图像大小超过5MB,我将退出内存错误…我已尝试 android:largeHeap="true" 但这并没有帮助我解决这个问题。我的java代码和logcat如下所示 public File addToDiskCache(String imageName, Bitmap inImage) { Log.e("Add to Disk",imageName); ByteArrayOutputStream b
android:largeHeap="true"
但这并没有帮助我解决这个问题。我的java代码和logcat如下所示
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;
}
日志:
07-09 21:10:47.482:E/art(4870):抛出OfMemoryError“失败”
分配6391674字节分配,6004224个空闲字节和5MB
直到“OOM”
07-09 21:10:47.482:D/AndroidRuntime(4870):关闭虚拟机
07-09 21:10:47.522:E/AndroidRuntime(4870):致命异常:主
07-09 21:10:47.522:E/AndroidRuntime(4870):进程:com.karopass.status2017,PID:4870
07-09 21:10:47.522:E/AndroidRuntime(4870):java.lang.OutOfMemory错误:分配6391674字节失败
分配6004224个可用字节和5MB,直到OOM
07-09 21:10:47.522:E/AndroidRuntime(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)
我的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;
}
请帮我解决这个问题
谢谢压缩无效 使用文件中的位图时,请尝试此操作
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;
}
可能与位图重复,可能位图太大,或者可能不是此方法造成的,请检查其他代码,查看是否忘记回收对象,尤其是位图。