Hassan Shahbaz February 2016

Show captured image in new activity

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

Answers


Mustanser Iqbal February 2016

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.


Raaja SN February 2016

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.


user5716019 February 2016

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.


androidnoobdev February 2016

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

}


Fabio Venturi Pastor February 2016

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

        }
     }

Post Status

Asked in February 2016
Viewed 3,203 times
Voted 13
Answered 5 times

Search




Leave an answer