0

I'm doing a photo editor app in android and I have two image buttons(one for the camera and the other for gallery). When I capture the photo with the camera or select the photo from the gallery I want the photo to be displayed in another activity on image view. I just wrote some of the code for the camera,but it doesn't work. If someone can help me , I'll be very grateful. I'm a newbie to programming.

manifest.xml

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

first activity:

 public class MainActivity extends Activity {
 private static int IMG_RESULT = 1;

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageButton take_photo = (ImageButton) findViewById(R.id.cameraButton);
    ImageButton get_photo  = (ImageButton) findViewById(R.id.galleryButton);
    take_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,0);

        }
    protected void onActivityResult(int requestCode, int resultCode, Intent  data) {
    if(requestCode == 0 && resultCode == RESULT_OK)
    {
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
        Intent intent = new Intent(this,ShowPhotoActivity.class);
        intent.putExtra("BitmapImage",bitmap);`
        startActivity(intent);`

second activity:

 public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_show_photo);
          ImageView showPhoto = (ImageView) findViewById(R.id.imageView);
          Bitmap bitImage = getIntent().getParcelableExtra("BitmapImage");
          showPhoto.setImageBitmap(bitImage);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
G.Al
  • 85
  • 7
  • What is the actual problem? Does your camera open? Are you able to capture the image. Any log errors? – Naz141 Sep 06 '16 at 00:55
  • the camera opens, i can capture the image,but it doesnt show it in another activity in imageview. – G.Al Sep 06 '16 at 01:01
  • Please can you post the xml of the second activity you want to open the image, i want to see the `` – Naz141 Sep 06 '16 at 01:02
  • Yes,sure .. – G.Al Sep 06 '16 at 01:05
  • Your code works fine. remove the single quotation in here ` intent.putExtra("BitmapImage",bitmap);` startActivity(intent);`` – Naz141 Sep 06 '16 at 01:16
  • Thats just a mistake I made when i copied the code...anyway,thank you . if you have any suggestion how to get the photo from gallery and do the same thing like with the camera image,I'll be very grateful :) – G.Al Sep 06 '16 at 01:27
  • please let me know if the below answer works for you – Naz141 Sep 06 '16 at 01:30

1 Answers1

0

Code tested and works fine.

Add this on manifest file

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

First Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 0 && resultCode == RESULT_OK){
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
        Intent intent = new Intent(this,ShowPhotoActivity.class);
        intent.putExtra("BitmapImage",bitmap);
        startActivity(intent);
    }
}

Second Activity

ImageView showPhoto = (ImageView) findViewById(R.id.imageView);
Bitmap bitImage = getIntent().getParcelableExtra("BitmapImage");
showPhoto.setImageBitmap(bitImage);

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imageView"
    android:adjustViewBounds="true"
    android:layout_width="match_parent"
    android:layout_height="300dp" />
</LinearLayout>
Naz141
  • 433
  • 1
  • 8
  • 31
  • Thank you..It works now,but when the photo is displayed in imageview is blurred.. – G.Al Sep 06 '16 at 01:34
  • Check the answer to avoid low quality image in [Here](http://stackoverflow.com/questions/10377783/low-picture-image-quality-when-capture-from-camera) – Naz141 Sep 06 '16 at 01:39
  • Please mark it as an answer or upvote if it worked for you (y) – Naz141 Sep 06 '16 at 01:56