我正在研究相机应用程序.第一次,如果我捕获图像其工作正常,但如果我再次拍照,它会抛出一个错误
ERROR/dalvikvm-heap(2398):10077696字节的外部分配对于这个过程来说太大了."VM不会让我们分配10077696字节",最后是"05-02 05:35:38.390:ERROR/AndroidRuntime(2398):致命异常:主05-02 05:35:38.390:错误/ AndroidRuntime(2398):java.lang.OutOfMemoryError:位图大小超过VM预算
和应用程序强制关闭..如何处理这个如何清除堆和虚拟机?请帮忙..在此先感谢..
我正在编写一个程序,该程序使用图库中的图像,然后在活动中显示它们(一个图像与活动)。然而,我连续三天一遍又一遍地遇到这个错误,却没有任何消除它的进展:
07-25 11:43:36.197: ERROR/AndroidRuntime(346): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Run Code Online (Sandbox Code Playgroud)
我的代码流程如下:
当用户按下按钮时,会触发一个意图,通向图库:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 0);
Run Code Online (Sandbox Code Playgroud)
一旦用户选择了图像,图像就会显示在图像视图中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:background="#ffffffff"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:maxWidth="250dip"
android:maxHeight="250dip"
android:adjustViewBounds="true"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在 onActivityResult 方法中我有:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case 0: // Gallery
String realPath = getRealPathFromURI(data.getData());
File imgFile = new File(realPath);
Bitmap myBitmap;
try {
myBitmap = decodeFile(imgFile); …Run Code Online (Sandbox Code Playgroud) 我昨晚发了一个关于这个问题的问题,但我不认为我解释得很好,可以得到适当的帮助.所以基本上,我有一个应用程序,您可以按下一个按钮,让您从图库中选择一个图像,并在我在应用程序中显示的ImageView中显示它.一切都很好.但是,当我再次按下按钮并选择不同的图片时,应用程序强制关闭.
更新现在,如果我从库中的下载照片文件夹中拍摄照片,它可以正常工作,可以随时切换照片.但当我回到我的相机照片文件夹来更改图片时,它会强行关闭.
更新2它也适用于我的Gallery应用程序中的任何其他文件夹.只有一个有这个问题(强制关闭)在"相机"文件夹上
更新3 19660800字节的外部分配对于此过程来说太大了.造成了这个问题.有什么方法可以减少所选照片的尺寸?
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.myimageview);
((Button) findViewById(R.id.taptoadd))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image …Run Code Online (Sandbox Code Playgroud) 我ViewFlow在我的应用程序中使用自己的Android实例.我正在从Web服务下载加密图像,而不是将它们保存在SD卡上.我正在使用viewflow来动态解密图像并显示它们.但问题是,当用户开始更快地更改图像时,它会给我一个OutOfMemoryException信息,而我发现/测试的所有信息对我的情况都不起作用.这是我正在使用的:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_item, null);
}
try {
File bufferFile = new File(ids.get(position));
FileInputStream fis = new FileInputStream(bufferFile);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
BitmapFactory.Options o = new BitmapFactory.Options();
final int REQUIRED_SIZE=300*1024;
//Find the correct scale value. It should be the power of …Run Code Online (Sandbox Code Playgroud) 我有一个listview,可能无限滚动加载无限项.
列表视图中的每个项目都有一个或两个我懒得加载的图像.
一切都很好但是当我滚动很长时间它会在log cat中崩溃
08-07 15:26:25.231: E/AndroidRuntime(30979): FATAL EXCEPTION: Thread-60
08-07 15:26:25.231: E/AndroidRuntime(30979): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-07 15:26:25.231: E/AndroidRuntime(30979): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-07 15:26:25.231: E/AndroidRuntime(30979): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
08-07 15:26:25.231: E/AndroidRuntime(30979): at com.test.android.helper.LazyImageLoader.decodeFile(LazyImageLoader.java:171)
08-07 15:26:25.231: E/AndroidRuntime(30979): at com.test.android.helper.LazyImageLoader.getBitmap(LazyImageLoader.java:112)
08-07 15:26:25.231: E/AndroidRuntime(30979): at com.test.android.helper.LazyImageLoader.access$2(LazyImageLoader.java:106)
08-07 15:26:25.231: E/AndroidRuntime(30979): at com.test.android.helper.LazyImageLoader$ImageLoader.run(LazyImageLoader.java:197)
Run Code Online (Sandbox Code Playgroud)
在我的懒惰图像加载器中,我将位图存储在a中WeakHashMap.垃圾收集器应该收集位图吗?
我懒惰的图像加载器就是这样的.
我displayImage()从我的适配器调用url和imageview的引用
public void displayImage(String url, ImageView imageView, int defaultImageResourceId){
latestImageMetaData.put(imageView, url);
if(weakhashmapcache.containsKey(url)){
imageView.setImageBitmap(weakhashmapcache.get(url));
}
else{
enqueueImage(url, imageView, defaultImageResourceId);
imageView.setImageResource(defaultImageResourceId);
}
}
Run Code Online (Sandbox Code Playgroud)
所以如果我在缓存中找到图像,我会直接设置它,否则我会用函数对它进行排队enqueueImage(). …
我试图从InputStream调整一个图像的大小,所以我在将图像加载到Bitmap对象时使用Strange内存中的代码问题,但我不知道为什么这个代码总是返回没有图像的Drawable.
这个很好用:
private Drawable decodeFile(InputStream f){
try {
InputStream in2 = new BufferedInputStream(f);
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=2;
return new BitmapDrawable(BitmapFactory.decodeStream(in2, null, o2));
} catch (Exception e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这个不起作用:
private Drawable decodeFile(InputStream f){
try {
InputStream in1 = new BufferedInputStream(f);
InputStream in2 = new BufferedInputStream(f);
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in1,null,o);
//The new size we want to scale to
final int IMAGE_MAX_SIZE=90;
//Find …Run Code Online (Sandbox Code Playgroud) 我的问题
我用我的android设备拍照.然后我从文件中解码该图片.
Bitmap photo = BitmapFactory.decodeFile(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
if (photo == null && data != null)
photo = (Bitmap) data.getExtras().get("data");
else if (data == null && photo == null)
Log.e("CCPhotoManager","Can't find image from file or from intent data.");
Run Code Online (Sandbox Code Playgroud)
然后,我检查该图片,看看是否需要将其旋转到正确的方向.
try {
ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
Log.v("CCPhotoManager", "Rotation:"+rotation);
if (rotation > 0) {
photo = this.convertSavedImageToCorrectOrientation(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX, photo, rotation);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如果它确实需要旋转我调用这个方法.
public Bitmap convertSavedImageToCorrectOrientation(String filePath,Bitmap photo,int rotation) …Run Code Online (Sandbox Code Playgroud) 这不是Android开发者控制台中神秘堆栈跟踪的重复(位图大小超过32位)
这个问题没有提供单行代码,也没有答案; 此外,即使我将位图大小设置为32x32,我也会收到此错误,它是唯一的Bitmap,因此它与内存无关.
我想做的事
所述Log.e用于ImageView的大小,因此位图输出为:
Width: 272
Height: 136
Run Code Online (Sandbox Code Playgroud)
怎么了
以下代码适用于Nexus4,Nexus7和Desire HD(CM10),但在模拟器上运行应用程序会给出如下所示的错误(API 8).
发现
我尝试过它的一半大小和32x32,这给出了同样的错误.
我在DialogFragment中显示ImageView(针对API 8的ActionBarSherlock/HoloEveryWhere),也许这就是罪魁祸首?
ImageView的:
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="12dp"
android:layout_marginTop="12dp" />
Run Code Online (Sandbox Code Playgroud)
在我的活动中:
iv = (ImageView) view.findViewById(R.id.imageView1);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (!waveFormMeasured) {
if (iv.getMeasuredWidth() > 1) {
width = iv.getMeasuredWidth();
Log.e(TAG, "Width: " + width + " Height: " + width / 2);
waveBitmap = Bitmap.createBitmap((int) width, …Run Code Online (Sandbox Code Playgroud) 我从我的资源文件夹中获取了一些图像,我有这个例外:
03-11 10:18:28.019: E/dalvikvm-heap(4052): Out of memory on a 9830416-byte allocation.
Run Code Online (Sandbox Code Playgroud)
我在这里有这个错误:
//stream to get photo
InputStream bitmap=null;
bitmap=getResources().getAssets().open("ProduitsMini/"+productList.get(rang).getImg_mini());
Bitmap bit=BitmapFactory.decodeStream(bitmap);
// get drawable image
Drawable mDrawable = new BitmapDrawable(getResources(),bit);
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为我在每台设备上都没有这个错误,只有Galaxy S3.
我有一个自定义网格视图的代码,每次我尝试运行它时,它都会因OutOfMemoryException. 我想解决方案是调整数组中的图像大小...
主要活动代码:
public class AndroidGridLayoutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
} …Run Code Online (Sandbox Code Playgroud)