问题描述:

How to take pic from camera and send image to second activity and in second activity image will be shown on image view.kindly give me the code.

i will be very thankful.

网友答案:

You should pass the informantion on the Intent as an Extra.

An example:

startActivity(new Intent(PresentActivity.this, NextActivity.class).putExtra("Key", "Value"));

More specifically for taking a picture and showing it on an ImageView, use an startActivityForResult. Example code:

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), REQUEST_CODE);

This will call a new activity which will take a picture, then you'll only need to call the onActivityResult method on the same activity you called the previous code and handle the result (take the picture uri and set it on the ImageView).

网友答案:
Bitmap capturedImage = // set your image here
File tempDir = this.getCacheDir();
File tempFile = null;
FileOutputStream fos = null;
try {
    tempFile = File.createTempFile("temp", ".png", tempDir);
    fos = new FileOutputStream(tempFile);

    if (capturedImage.compress(Bitmap.CompressFormat.PNG, 85, fos)) {
        fos.flush();
        fos.close();
        Uri fileUri = Uri.fromFile(tempFile);
        result.putExtra("fileUri", fileUri);
        this.setResult(Activity.RESULT_OK, result);
        this.finish();
    } else {

    }
} catch (IOException ioe) {
    ioe.printStackTrace();
}

you can't put image to intent. intent extra has size limit of 1MB.

just save your image to temp file and send it's uri.

网友答案:

Have a look at the putExtra() and the getExtra() method. Here are some code snippets:

//First Acitivity
Bitmap img=//set here the image you got from the camera.


Bundle extras = new Bundle();
extras.putParcelable("image", img);
intent.putExtras(extras);
startActivity(intent);

//Second Activity
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("image");

If you didn't already think about, how to start the camera and get the image, I'd suggest you to use startActivityForResult().

相关阅读:
Top