2011-11-09 17 views
0

こんにちは私はAndroidでカメラアプリケーションを使用しています。 PictureCallbackメソッドのバイトデータを別のアクティビティに渡し、そのアクティビティに表示したいと思います。これはAndroidでカメラで撮影した画像を次の画面に表示する方法

<?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> 

私はこのコードを実行している私のレイアウトmain2.xmlある

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); 
    } 
} 

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); 
     } 
    }; 

と怒鳴る示すように、第2のクラスview.class、:私は、次のコードを使用しました、カメラがクリックされた後にforce_closeが発生します。もし誰かがそれについて知っていれば私を助けてください。あなたのカメラの活動を開始する場所をこのコードが挿入されなければならない

答えて

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()); 
      } 
     } 
    } 

} 
+0

こんにちは、私のアプリケーションでは、私はイメージをSDカードに保存したくないです。キャプチャした画像を別のアクティビティに渡してそこに表示したいだけです。上記のコードを実行しました。そのコードでは、イメージのバイトデータを2番目のアクティビティにアクセスできます。私はそれをビットマップイメージに変換し、それをイメージビューに表示したいと思います。しかし、エラーが発生します。あなたはそれについて知っていますか? – Binu

+0

おそらくdecodeByteArray()が解読できない場合は、エラーが発生する可能性があります。何かを見つけたらお知らせします:) – rohit

+0

あなたはエミュレータまたはデバイスを使用していますか?あなたができる場合は、デバイス上でそれを試してください – rohit

関連する問題