2011-08-31 90 views
2

我有一個activity A我正在建造我自己的相機。在這個活動中,相機被打開,當使用按下按鈕拍攝照片時。使用安卓相機拍了幾張照片後出錯

這是這樣完成的:

Activity A: 
    //this button needs to be pressed to take the photo 

    public void onClick(View arg0) { 
    mCamera.takePicture(null, mPictureCallback, mPictureCallback); 
      } 

    //the method that gets called 
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 
    public void onPictureTaken(byte[] imageData, Camera c) { 

     if (imageData != null) { 
      Intent mIntent = new Intent(); 


      Bundle b = new Bundle(); 
      b.putByteArray("imageData", imageData); 
      Intent i = new Intent(mContext, ViewPhoto.class); 
      i.putExtras(b); 
      startActivity(i); 

      setResult(FOTO_MODE, mIntent); 
      finish(); 

     } 
    } 
}; 

圖片發送到第二個活動,用戶可以查看它,看看它是否喜歡它或not.If他不喜歡這樣,他可以再重拍照片

活性B:

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
       //.......... 
     Bundle extras = getIntent().getExtras(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 2; 
    byte[] imageData = extras.getByteArray("imageData"); 
    Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, 
      imageData.length, options); 

    Matrix mat = new Matrix(); 
    mat.postRotate(90); 
    bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), 
      myImage.getHeight(), mat, true); 

       //....... 
       } 

在該第二活動我接收畫面的字節,並創建一個第一bitmap

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, 
       imageData.length, options); 

之後,我需要旋轉first bitmap創建第二位:用戶看到bitmapResult

bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), 
       myImage.getHeight(), mat, true); 

後,他可以重新拍照。

backTakePhoto.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 

      //...back to activity A 
     } 
    }); 

的問題是,服用了幾張照片後,循環,如:

activity A->activity B->activity A->Activity B my app crashes in activity B at this line: 

bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), 
       myImage.getHeight(), mat, true); 

,這是我的logcat的樣子:

FATAL EXCEPTION: main 
java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
at android.graphics.Bitmap.nativeCreate(Native Method) 
at android.graphics.Bitmap.createBitmap(Bitmap.java:477) 
at android.graphics.Bitmap.createBitmap(Bitmap.java:444) 
at com.Xperiaproject.ViewPhoto.onCreate(ViewPhoto.java:71) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) 
at android.app.ActivityThread.access$1500(ActivityThread.java:121) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:3701) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
at dalvik.system.NativeStart.main(Native Method) 
Force finishing activity com.Xperiaproject/.ViewPhoto 

任何想法?

+1

如果U R感興趣,我會給你完整的圖像捕捉代碼使用相機,並在應用程序中使用。 –

+0

那麼你可以給我,爲什麼不。 – adrian

回答

4

您沒有足夠的內存來創建您的位圖。這就是它崩潰的原因。

爲了釋放你的內存,你應該在未使用的時候回收你的位圖或者將它保存到sd卡然後回收。

bitmapResult.recycle(); 
2

在按鈕上單擊事件編寫以下代碼。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

onActivityResult方法: -

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == CAMERA_PIC_REQUEST) { 
       System.out.println("Dipak Keshariya"); 
       drawable=null; 
       bmpImage=null; 
       bmpImage = (Bitmap) data.getExtras().get("data"); 
       System.out.println("Image Path is:- " +data.getExtras().get("data")); 
       drawable = new BitmapDrawable(bmpImage); 
       mImageviewmain.setVisibility(View.VISIBLE); 
       mImageviewmain.setImageDrawable(drawable); 
      } 
     } 
} 

申報以下變量/對象Globbally: -

位圖bmpImage;

可繪製的drawable;

+0

我讚賞你的努力,但做到了這一點。這不好! – adrian

+0

這是什麼問題? –

+0

U可以使用像這樣的默認相機。但是一些移動設備在拍攝照片後會旋轉它或者將其保存錯誤。並且爲了克服這個問題,您必須建立自己的相機。我很幸運,您的移動設備可以保存圖像而且你不必建立你自己的相機。 – adrian

相關問題