Hi I'm using a camera application in Android. I want to pass the byte data from PictureCallback method to another activity and want to display it in that activity. I used the following code:
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);
}
};
and second class view.class as shown bellow,
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);
}
}
This is my layout 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>
when I am running this code, force_close
occurs after the camera click. If anyone knows about it please help me.
This code should be inserted where you start your camera activity .. like say on some button click.. it ll open your camera-
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);
}
You can then override onActivityResult method like this -
@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());
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.