我試着寫一些相機意向處理程序,拍照和後旋進他們(如果需要)。如果我在高分辨率拍攝一些照片,我的程序中斷「分配過大」。如果我採取一些較低的分辨率我能夠採取更多,但它也會打破。經過一番搜索後,我發現我必須回收位圖,而且我已經制作了手冊。但問題並沒有消失。我的主要問題是,我不知道是否有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)
任何幫助將是巨大的。
問候亞歷
編輯:我忘了說,當我回到privios活動和/或啓動其他意圖出現問題。 – Alex