如何在android中将相机捕获的图像显示到下一个屏幕

时间:2011-11-09 08:26:44

标签: android image bitmap

您好我在Android中使用相机应用程序。我想将字节数据从PictureCallback方法传递到另一个活动,并希望在该活动中显示它。我使用了以下代码:

Camera.PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Intent i = new Intent(FbCamera.this, view.class);
               i.putExtra("photo", data);
               Log.d(TAG, "jpegCallback" +data);
               startActivity(i);
        }
    };

和第二类view.class如下所示,

public class view extends Activity {    
    private static final String TAG = "Camera";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Bundle extras = getIntent().getExtras();
        byte[] photo = extras.getByteArray("photo");
        Log.i(TAG, "jpegCallback2" + photo);
        Bitmap bitmap  = BitmapFactory.decodeByteArray (photo, 0, photo.length);
        ImageView imgView = (ImageView)findViewById(R.id.photoResultView);
        imgView.setImageBitmap(bitmap);
    }
}

这是我的布局main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:layout_height="fill_parent" 
           android:id="@+id/photoResultView" 
           android:src="@drawable/icon" 
           android:layout_width="fill_parent"></ImageView>
</LinearLayout>

当我运行此代码时,单击相机后会出现force_close。如果有人知道,请帮助我。

1 个答案:

答案 0 :(得分:0)

此代码应插入您开始相机活动的位置..就像点击一些按钮时说的那样..它会打开相机 -

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    //You save your image in your sdcard by name "Pic.jpg"
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);  //Save uri that is path of your image
    startActivityForResult(intent, TAKE_PICTURE);
}

然后你可以像这样覆盖onActivityResult方法 -

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri; //Get image path                          
            getContentResolver().notifyChange(selectedImage, null);
            btn = (ImageButton) findViewById(R.id.camera);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                btn.setImageBitmap(bitmap); // I have displayed that image on imagebutton
                 // you can display anyware you want like imageview
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }

}