2

Hi stackoverflow friends, I need to take a picture using camera and after takin the picture go to next activity without showing the first activity. And display it in an imageview. Below is the flow of my application

first activity-> there is button for camera intent->go to the next activity(without showing the fist activity) second activity->there i need to show the image in imageview.

I saw a lot of examples of camera intent nobody explains how to go to the next activity without showing the first and display it in imageview of second.

Any outofmemeory problem occurs while displaying images in imageview repeatedly?

Thanks in advance

Asish AP
  • 4,421
  • 2
  • 28
  • 50

3 Answers3

1

In First activity :

 Button b=(Button)findViewByid(R.id.button);
 b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    doTakePhotoAction();

    }
}); 
    private void doTakePhotoAction() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
    "pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

try {
    intent.putExtra("return-data", true);
    startActivityForResult(intent, CAMERA_RESULT);
       // finish();
} catch (ActivityNotFoundException e) {
    e.printStackTrace();
}
}

 protected void onActivityResult(int requestCode, 
    int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
    return;
}
if (requestCode == CAMERA_RESULT) {
    Intent intent = new Intent(this, nextimage.class);
    // here you have to pass absolute path to your file
    intent.putExtra("image-path", mUri.getPath());
    intent.putExtra("scale", true);
    startActivity(intent);
        finish();
}
}

In nextimage.class you can set one image view and get the imagepath from putExtra and place it in imageview.

  String mImagePath = extras.getString("image-path");
  Bitmap mBitmap = getBitmap(mImagePath);
   private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}

private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
    in = mContentResolver.openInputStream(uri);
    return BitmapFactory.decodeStream(in);
} catch (FileNotFoundException e) {
    Log.e(TAG, "file " + path + " not found");
}
return null;
}

place the bitmap in the imageview.you have to create imageview in secondActivity.

deepa
  • 2,496
  • 1
  • 26
  • 43
  • Thanks deepa, your answer is almost ok but in need to to save my image internally (i mean a file)and after that in need to get that path.So that each time when i taken the picture the old picture will delete.Have any solution. – Asish AP Jul 22 '11 at 12:18
  • 2
    I didnt use the code for saving but the following link may help you. http://stackoverflow.com/questions/649154/android-bitmap-save-to-location – deepa Jul 23 '11 at 04:22
  • http://www.brighthub.com/mobile/google-android/articles/64048.aspx try this link it may help you. – deepa Jul 23 '11 at 04:29
0

use a camera intent....here's a simple code

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.widget.ImageView;

public class CameraIntent extends Activity {

final static int CAMERA_RESULT = 0;

ImageView imv;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(i, CAMERA_RESULT);

}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

super.onActivityResult(requestCode, resultCode, intent);

if (resultCode == RESULT_OK)

{

Get Bundle extras = intent.getExtras();

Bitmap bmp = (Bitmap) extras.get("data");

imv = (ImageView) findViewById(R.id.ReturnedImageView);

imv.setImageBitmap(bmp);

}

}

}

RMT
  • 7,040
  • 4
  • 25
  • 37
user632905
  • 990
  • 9
  • 19
  • hi user632905 thanks for your quick response. here i need to display the image to imageview in the next activity without showing the first activity . How can i possible for this ? – Asish AP Jul 22 '11 at 12:03
  • Is it possible to set the image to imageview of the second activity from first activity? – Asish AP Jul 22 '11 at 12:10
  • its simple.....create a function getbitmap in first activity in which u return the bitmap bmp in this case....now in second activity create an object of first activity and call the method getbitmap so now u have that bitmap in your second activity.....now u can use it in any way u want – user632905 Jul 22 '11 at 12:30
0

Use Following Code for that, it will solve your problem.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

onActivityResult() Method:-

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            bmpImage = (Bitmap) data.getExtras().get("data");
            drawable = new BitmapDrawable(bmpImage);
            mImageview.setImageDrawable(drawable);
        }
    }
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128