Java 内存不足异常
我是android新手。我正在做sd卡的图像列表。下面的代码告诉我错误“614416字节分配的内存不足” 类别:Java 内存不足异常,java,android,exception,out-of-memory,Java,Android,Exception,Out Of Memory,我是android新手。我正在做sd卡的图像列表。下面的代码告诉我错误“614416字节分配的内存不足” 类别: class AsyncImageLoaderv { int width; int height; float aspectRatio; int newWidth; int newHeight; public Bitmap loadDrawable(final String imageUrl, final Im
class AsyncImageLoaderv {
int width;
int height;
float aspectRatio;
int newWidth;
int newHeight;
public Bitmap loadDrawable(final String imageUrl,
final ImageCallback imageCallback) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
try {
Log.d("ur", imageUrl);
Bitmap drawable = BitmapFactory.decodeFile(imageUrl);
width = drawable.getWidth();
height = drawable.getWidth();
aspectRatio = (float) width / (float) height;
newWidth = 98;
newHeight = (int) (98 / aspectRatio);
Bitmap.createScaledBitmap(drawable, newWidth, newHeight,
true);
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
//this.sleep(1000);
} catch (Exception e) {
Log.e("thread stellent", e.toString());
}
}
}.start();
return null;
}
public interface ImageCallback {
public void imageLoaded(Bitmap imageBitmap, String imageUrl);
}
}
正如我在该问题中回答的那样:
位图无法保存重量级图片。阅读这篇文章,可以减小图像的大小。检查图像是否沉重……此错误表明您正在尝试加载沉重的图像。您正在将图像加载到内存中,然后缩放图像。您应该使用采样中的
inSampleSize
对其进行缩放。有许多重复的问题显示了如何解决这个问题。和android文档。
class AsyncImageLoaderv {
int width;
int height;
float aspectRatio;
int newWidth;
int newHeight;
public Bitmap loadDrawable(final String imageUrl,
final ImageCallback imageCallback) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
try {
Log.d("ur", imageUrl);
Bitmap drawable = BitmapFactory.decodeFile(imageUrl);
width = drawable.getWidth();
height = drawable.getWidth();
aspectRatio = (float) width / (float) height;
newWidth = 98;
newHeight = (int) (98 / aspectRatio);
Bitmap.createScaledBitmap(drawable, newWidth, newHeight,
true);
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
//this.sleep(1000);
} catch (Exception e) {
Log.e("thread stellent", e.toString());
}
}
}.start();
return null;
}
public interface ImageCallback {
public void imageLoaded(Bitmap imageBitmap, String imageUrl);
}
}