Question

I have two Activities first activity has the dynamic image view and on click i am calling the second activity where camera in invoked to capture a image, now i am able to invoke camera and capture a image but how would i pass that image to the previous activity and set to the image view.

String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, IMAGE_CAPTURE);
Was it helpful?

Solution

Finally I achieve using two activities with the help of startActivityForResult.

check below piece code, hope its helpful you:

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="57dp"
        android:layout_marginTop="84dp"
        android:text="Button" />


     <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         />

</RelativeLayout>

FirstActivity.java

It will open second activity with startActivityResult

package com.example.demo_cameraintent;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image=(ImageView)findViewById(R.id.image);

        String extStorageDirectory = Environment.getExternalStorageDirectory()
                + "/testing";

      File xmlDirectory = new File(extStorageDirectory);
        if (!xmlDirectory.exists())
            xmlDirectory.mkdirs();

        Button btn=(Button)findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, 1);

            }
        });
    }

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

        if (requestCode == 1) {

             if(resultCode == RESULT_OK){

              String result=data.getStringExtra("result");
              Log.d("*****************", "inside onactivityresult in main activity="+result);

            Bitmap bitmap = BitmapFactory.decodeFile(result);
            image.setImageBitmap(bitmap);
            image.setScaleType(ScaleType.FIT_XY);

        }

        if (resultCode == RESULT_CANCELED) {



             //Write your code on no result return 

        }
        }//onAcrivityResult


    }
}

SecondActivity.java

Here when its called it will directly open cameraIntent and within onActivityResult it will return back to Firstactivity with your output

package com.example.demo_cameraintent;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;

public class SecondActivity extends Activity {
    String filepath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

         filepath = Environment.getExternalStorageDirectory()+"/testing/"+"test.jpg";
        System.out.println("thumbnail path~~~~~~"+filepath);
        File file = new File(filepath);
        Uri outputFileUri = Uri.fromFile(file);


        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 100);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 100) {

                 Intent returnIntent = new Intent();
                 returnIntent.putExtra("result",filepath);
                 setResult(RESULT_OK,returnIntent);     
                 finish();
        }


    }



}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo_cameraintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo_cameraintent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

</manifest>

OTHER TIPS

Seem you are trying to capture image from camera and setting on your view, But I think you don't need two activities to achieve your goal.

You can achieve in single activity with the help of startActivityForResult.

Simply call Camera Intent on click of imageview, and get back your result within same activity, check below code:

Define path for capture image:

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename;
System.out.println("thumbnail path~~~~~~"+filepath);
File file = new File(filepath);
Uri outputFileUri = Uri.fromFile(file);

code this on click of imageview:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 100);

onActivityResult in same activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        bitmap=GlobalMethods.decodeFile(_path);//function to decode.
        if (bitmap == null) {
            ivPhy.setImageBitmap(bitmap);
        } 
        else {
            ivPhy.setImageBitmap(bitmap);
            ivPhy.setScaleType(ScaleType.FIT_XY);
        }
    }


}

Required permission pass into manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Instead of using ContentValues for creating imageUri, just store your image in sdcard like this:

String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/testphoto.jpg";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)) ); 

After that, while calling second activity just pass the path of file like this:

intent.putExtra("pathOfImage",path);

Now, on second activity you can just retrieve the image from sdcard:

File f = new File(getIntent().getStringExtra("pathOfImage"));
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top