返回相机图像时出现绑定器事务失败

11

当我使用相机意图(通过putExtra将拍摄的图像返回给父意图作为byte[])返回图像时,在logcat中出现了“Failed binder transaction”错误。我不明白为什么会这样,毕竟它并不是一个大位图或其他什么东西。只有在拍摄光线很亮的照片时才会发生,因为这时byte[]更大。该错误发生在离开相机意图时。有人看到我的代码有什么错误吗?

这是相机意图的代码:

package example.imaging.ape;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;

public class TakePicture extends Activity implements SurfaceHolder.Callback{
     Camera mCamera;
     Boolean mPreviewRunning = false;
     int imageLayoutHeight;
     int imageLayoutWidth;

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          //setup camera surface
          getWindow().setFormat(PixelFormat.TRANSLUCENT);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.cameralayout);

          SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.hist_surface_camera);
          SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
          mSurfaceHolder.addCallback(this);
          mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          Bundle extras = getIntent().getExtras();
          imageLayoutHeight = extras.getInt("layoutHeight");
          imageLayoutWidth = extras.getInt("layoutWidth");

          OnTouchListener touchListener = new View.OnTouchListener() {
               public boolean onTouch(View v, MotionEvent e) {

                    System.out.println("MAKING PICTURE");
                    mCamera.autoFocus(cb);             
                    return false;
               }
          };

          //setup touch listener
          mSurfaceView.setOnTouchListener(touchListener);

     }

     AutoFocusCallback cb = new AutoFocusCallback() {
          public void onAutoFocus(boolean success, Camera c) {
               c.takePicture(null, null, mPictureCallback);
          }   
     };

     Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
          public void onPictureTaken(byte[] imageData, Camera c) {
               System.out.println("Picture taken, now returning");              
               Intent resultIntent = new Intent();
               resultIntent.putExtra("cameraImage", imageData);
               System.out.println("put Extra");
               setResult(Activity.RESULT_OK, resultIntent);
               finish();           
          }
     };

     //initialize camera
     public void surfaceCreated(SurfaceHolder holder) {
          mCamera = Camera.open();
     }

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          if (mPreviewRunning) {
               mCamera.stopPreview();
          }

          Camera.Parameters p = mCamera.getParameters();

          p.setPreviewSize(h, w);
          System.out.println("PreviewSize: " + h + "," + w);
          p.setPictureSize(h*3,w*3); // is around 1200x900
          p.set("rotation", 90);
          mCamera.setParameters(p);

          try {
               mCamera.setPreviewDisplay(holder);
          } catch (IOException e) {
               e.printStackTrace();
          }

          mCamera.startPreview();
          mPreviewRunning = true;
     }

     public void surfaceDestroyed(SurfaceHolder holder) {
          mCamera.stopPreview();
          mPreviewRunning = false;
          mCamera.release();
     }

}

以下是调用相机Intent的代码:

Intent intent = new Intent(Example.this, TakePicture.class);
intent.putExtra("layoutWidth",layoutWidth);
intent.putExtra("layoutHeight",layoutHeight);                   
startActivityForResult(intent,0);

2
你最终解决了这个问题吗?我也遇到了同样的问题。 - Colin O'Dell
3个回答

17

由于某些原因,Android 不允许您尝试传递原始的 byte[] 数组或从中创建的 Bitmap。一些人已经成功地压缩了结果 Bitmap 并通过 Intent 传递它。我建议先将图像保存到文件中,然后通过 Intent 发送其路径。


确保返回数据为false。 - hunterp
2
@hunterp,你的意思是什么?我遇到了这个问题,需要解决。 - Metropolis

2
在远程过程调用期间,调用的参数和返回值被转换为存储在Binder事务缓冲区中的Parcel对象。如果参数或返回值太大而无法适应事务缓冲区,则调用将失败,并抛出TransactionTooLargeException异常。
请参考此链接:android developer

0

模拟器在意图中丢失内存加载属性,因此会发生异常


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接