The first activity has a button which when clicked, opens the inbuilt camera. Now when the picture is taken, a new activity opens with the image captured in an imageView
and a share button in the next activity. I have set up the activity to open after the image is taken, however I am unable to transfer image captured across activities. Please i need help or a nudge in the right direction.
The first activity which takes the picture is Takepicture.java
:
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.os.Environment;
import java.io.File;
import java.util.Date;
public class TakePicture extends Activity {
Button camerabutton;
Intent intent;
int requestCode;
int resultCode;
static int REQUEST_IMAGE_CAPTURE = 1;
SharedPreferences imagepreferences;
SharedPreferences.Editor imageeditor;
private String imgPath;
Uri setImageUri;
File file;
Uri imgUri;
public String getImagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takepicture);
camerabutton = (Button) findViewById(R.id.button6);
imagepreferences=getSharedPreferences("image", MODE_PRIVATE);
imageeditor=imagepreferences.edit();
camerabutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null)
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
startActivity(new Intent(TakePicture.this, Aftertakepicture.class));
}
}
}
the second activity, Aftertakepicture.java
:
package com.example.kesandunwokolo.febclasstest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Aftertakepicture extends Activity {
Button camerabutton;
ImageView saveimage;
Button sharebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aftertakepicture);
camerabutton=(Button)findViewById(R.id.button6);
saveimage=(ImageView)findViewById(R.id.imageView2);
sharebutton=(Button)findViewById(R.id.button7);
}
}
The takepicture.xml
for the first activity:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/button6" />
The aftertakepicture.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/button6" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:minHeight="100dp"
android:minWidth="100dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Share"
android:id="@+id/button7" />
</LinearLayout>
please any help would be appreciated!
there are several ways to do accomplish this, via sending the
bitmap
itself withinIntent
(Not Recommended) or you save the image to the storage and send its path within theintent
which is recommended. first you save the image to theSDCARD
and then pass it within theintent
for exampleand then in the other activity you could use
Here are my solution, I have tested in my environment. Hope this helps!
If using emulator to test, make sure camera supported like this
UPDATE WIH FULL SOURCE CODE (NEW PROJECT):
MainActivity.java:
SecondActivity.java:
activity_main.xml:
activity_second.xml:
AndroidManifest.xml:
END OF NEW PROJECT
------------------
FirstActivity:
SecondActivity:
Refer to this link. You should save your image on a file and get the file path of that image. You can then pass the image path to the second activity.