2012-05-09 62 views
2

我試着寫一些相機意向處理程序,拍照和後旋進他們(如果需要)。如果我在高分辨率拍攝一些照片,我的程序中斷「分配過大」。如果我採取一些較低的分辨率我能夠採取更多,但它也會打破。經過一番搜索後,我發現我必須回收位圖,而且我已經制作了手冊。但問題並沒有消失。我的主要問題是,我不知道是否有1.在我的代碼2.內存泄漏我不知道爲什麼它試圖分配momory因爲我沒有表現出我的PROGRAMM位圖(現在) ,我只是將它們保存起來以備後用。內存泄漏而位圖處理

public void onClick(View view) { 
    Intent i = new Intent("android.media.action.IMAGE_CAPTURE"); 
    this.mLastPic = new File(this.mFs.getDirPath(), this.mFs.getNextPicName()); //create and save a file for pic 
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.mLastPic)); 
    this.startActivityForResult(i, 0); 
} 

上activty結果處理

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == Activity.RESULT_OK && requestCode == 0) { 

     try { 
      if(!this.mController.getSetting(R.string.USE_HEIGH_RESOLUTION)) { //use high res or not 
       int quality = this.mFs.getPicQuality(); //get compress quality 
       Bitmap pic = BitmapFactory.decodeFile(this.mLastPic.getPath()); 
       ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
       pic.compress(Bitmap.CompressFormat.JPEG, quality, outStream); 
       String path = this.mLastPic.getPath(); 
       if(this.mLastPic.delete()) { //replace the old file with a now file 
        File newFile = new File(path); 
        newFile.createNewFile(); 
        FileOutputStream os = new FileOutputStream(newFile); 
        os.write(outStream.toByteArray()); 
        Log.d("newEntryActivity.onActivityResult", "replaced pic "); 
       } else { 
        Log.d("newEntryActivity.onActivityResult", "cant delete old pic"); 
       } 
       pic.recycle(); //cleaning up 
       outStream.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

logcat的顯示

05-09 14:35:01.694: E/dalvikvm-heap(845): 6380496-byte external allocation too large for this process. 05-09 14:35:01.694: E/(845): VM won't let us allocate 6380496 bytes

05-09 14:35:01.694: D/AndroidRuntime(845): Shutting down VM

05-09 14:35:01.694: W/dalvikvm(845): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

05-09 14:35:01.694: E/AndroidRuntime(845): Uncaught handler: thread main exiting due to uncaught exception

05-09 14:35:01.714: E/AndroidRuntime(845): java.lang.RuntimeException: Unable to start activity ComponentInfo{unicorn.Heurazio/unicorn.Heurazio.SettingsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class ...

05-09 14:35:01.714: E/AndroidRuntime(845): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.createView(LayoutInflater.java:513)

05-09 14:35:01.714: E/AndroidRuntime(845): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

05-09 14:35:01.714: E/AndroidRuntime(845): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.app.Activity.setContentView(Activity.java:1622)

05-09 14:35:01.714: E/AndroidRuntime(845): at unicorn.Heurazio.SettingsActivity.onCreate(SettingsActivity.java:38)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

05-09 14:35:01.714: E/AndroidRuntime(845): ... 11 more

05-09 14:35:01.714: E/AndroidRuntime(845): Caused by: java.lang.reflect.InvocationTargetException

05-09 14:35:01.714: E/AndroidRuntime(845): at android.widget.LinearLayout.(LinearLayout.java:92)

05-09 14:35:01.714: E/AndroidRuntime(845): at java.lang.reflect.Constructor.constructNative(Native Method)

05-09 14:35:01.714: E/AndroidRuntime(845): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.LayoutInflater.createView(LayoutInflater.java:500)

05-09 14:35:01.714: E/AndroidRuntime(845): ... 21 more

05-09 14:35:01.714: E/AndroidRuntime(845): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.Bitmap.nativeCreate(Native Method) > 05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.content.res.Resources.loadDrawable(Resources.java:1705)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.View.(View.java:1850)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.View.(View.java:1799)

05-09 14:35:01.714: E/AndroidRuntime(845): at android.view.ViewGroup.(ViewGroup.java:284)

任何幫助將是巨大的。

問候亞歷

+0

編輯:我忘了說,當我回到privios活動和/或啓動其他意圖出現問題。 – Alex

回答

0

我的程序的一些閱讀和修改後,就像在重載應用程序添加到背景參考,抹所有不需要的背景元素我還有同樣的問題,這似乎是甚至沃瑟。

如果我能相信我的計算爲位圖

Bitmap tmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo); 
Log.d("mainApp", tmp.getRowBytes() * tmp.getHeight()/1024 + "kB"); 

100kB標誌(巴紐)的大小需要6mB RAM(這將適合異常消息)。任何想法都會很棒。

+1

OK,finaly我發現的bug。在MAT的幫助下,我能夠弄清楚,我有兩個引用長期運行的上下文。 – Alex

-3

嘗試在你的清單中添加應用程序android:largeHeap="true"

+0

dosn't這只是推回問題? – Alex

+0

這不是處理內存泄漏的正確方法。 – sdasdadas

1

如果你檢查你的日誌,它看起來像充氣的同時在unicorn.Heurazio.SettingsActivity視圖出現大(〜6MB)位分配。檢查您的資源,看看您是否在某處使用大型位圖作爲背景。

+0

我在每個活動中都有背景圖片。它的大小約爲75kB。它作爲背景圖像放置在佈局中。 diden't android管理'em的創建和回收? – Alex

+1

文件大小並不總是與內存大小相同,尤其是如果它是JPG。是的,Android應該創建/回收/釋放它們,但有時並不是很擅長及時完成它,並且您需要手動解除視圖以幫助提示它。 – kabuko

+0

我如何解除視圖? sry爲那noobie問題... – Alex