2

I am new in android and building a small app which take picture from camera and save it to gallery.

Here is the function that capture image.

private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

        File destination = new File(Environment.getExternalStorageDirectory(),
                System.currentTimeMillis() + ".jpg");

        FileOutputStream fo;
        try {
            destination.createNewFile();
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

This is activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >

        <Button
            android:id="@+id/btnSelectPhoto"
            android:background="#149F82"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />
    </LinearLayout>



</LinearLayout>

What i want to do that when image is captured i want to display the image on another activity(page) not on the same activity which have button to capture image.How to do that.

Thanks in advance

Hassan Shahbaz
  • 596
  • 1
  • 14
  • 38
  • You need to save image to device than get path of captured image http://stackoverflow.com/questions/20327213/getting-path-of-captured-image-in-android-using-camera-intent and pass it to the needed activity – Volodymyr Feb 08 '16 at 08:14
  • 1
    You are already saving image in the external storage `File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");` just switch the activity and read file there. – Murtaza Khursheed Hussain Feb 08 '16 at 08:15
  • 1
    Get returned data from the picker intent in onactivityResult protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); Uri tempUri = getImageUri(getApplicationContext(), photo); } then pass that data to new activity using bundle and then display image from that bundle. – Wasim K. Memon Feb 08 '16 at 08:15
  • @ Murtaza Khursheed Hussain how can i switch the activity can you please elaborate in detail. – Hassan Shahbaz Feb 08 '16 at 08:18
  • Check my answer and tell me if works please – Fabio Venturi Pastor Feb 08 '16 at 08:39

5 Answers5

2

You just have to pass path to new activity from your method.

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("MyImagePath", destination.getAbsoluteFile());
startActivity(intent);

And in New Activity

File imgFile = new  File(filePath);

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
1

Its Simple, After Capturing the Photo, Your photo is going to be Saved. Now, On Activity Result method, You just have to use intent to go to your second Activity.

In your second Activity just get the Path of your saved image and set it in to your ImageView.

user5716019
  • 598
  • 4
  • 17
1

MainActivity

 String filePath;
    @Override
        protected void onCreate(Bundle savedInstanceState) {   
        ...
        }

    private void onCaptureImageResult(Intent data) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

      String fileName = System.currentTimeMillis() + ".jpg"

      filePath =  Environment.getExternalStorageDirectory().getAbsolutePath() + fileName;

      File destination = new File(filePath);

      ...
      }

      yourButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
                intent.putExtra("filePath", filePath)
                startActivity(intent);

                }
            });

AnotherActivity

String filePath;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

    Intent intent = this.getIntent();

    filePath = intent.getStringExtra("filePath");

    File imgFile = new  File(filePath);

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

        myImage.setImageBitmap(myBitmap);

        }
     }
Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32
0

I assume that you know how to get captured image information from intent.

in case if you dont know. google it and you will find plenty of solution for that.

check this one

You can adopt two ways to show captures image on next activty.

1 => start new activity in which you want to display image. and open the camera in onCreate method and get result from camera in onActivityResult and show the image.

2=> open the camera and and getIntent data in onActivityResult and pass this information to your next activity and show the image in onCreate method.

Community
  • 1
  • 1
Mustanser Iqbal
  • 5,017
  • 4
  • 18
  • 36
0

Below is a solution. You can pass the captured Bitmap into an intent and then start your new activity. Like this one below. You can Implement this logic inside your onCaptureImageResult() once you get the image form the capture.

    Intent intent = new Intent(this, NewActivity.class);
    intent.putExtra("MyImage", bitmap);
    startActivity(intent);

You can Then get the Bitmap from the intent following the below step. And set the image to an ImageView or do something else with it.

    ImageView imgView = (ImageView) img.findViewById(R.id.myIMageView); /* Called inside OnCreate() using the parent View */
    Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("MyImage");

I hope it helps you. Enjoy.

Raaja SN
  • 382
  • 3
  • 14