1

I've been trying to pass the image from gallery or from camera to another activity but they don't work properly. If the image from the gallery will work then the image from camera won't work and vice versa. Please help me fix my code. Here are my code for the MainActivity and for the SecondActivity. Thank you!

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button btnStart,btnGallery,btnAbout;
ImageView reademlogo, pianotiles,imgclouds,imgclouds2,imgsharp,imgfclef;
TextView txtread;
Typeface tf1;
private final int REQUEST_IMAGE_CAPTURE = 1;
private final int PICK_IMAGE = 1;
Uri imageUri;

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

    UserInterface();
}

 private void UserInterface() {
    btnStart = (Button)findViewById(R.id.btnStart);
    btnGallery = (Button)findViewById(R.id.btnGallery);
    btnAbout = (Button)findViewById(R.id.btnAbout);
    reademlogo = (ImageView)findViewById(R.id.reademLogo);
    pianotiles = (ImageView)findViewById(R.id.pianotiles);
    imgclouds = (ImageView)findViewById(R.id.imgclouds);
    imgclouds2 = (ImageView)findViewById(R.id.imgclouds2);
    imgsharp = (ImageView)findViewById(R.id.imgsharp);
    imgfclef = (ImageView)findViewById(R.id.imgfclef);

    txtread = (TextView)findViewById(R.id.txtread);

    tf1 = Typeface.createFromAsset(getAssets(),
          "fonts/VeganStylPersonalUse.ttf");
    txtread.setTypeface(tf1);
 }

 public void captureImage(View view) {
    Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (iCamera.resolveActivity(getPackageManager()) != null) {
         startActivityForResult(iCamera, REQUEST_IMAGE_CAPTURE);
     }
 }

 public void galImage(View view) {
    Intent iGallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    iGallery.setType("image/*");
    startActivityForResult(iGallery, PICK_IMAGE);
 }

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

    if(resultCode == RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE){

        Bitmap bitmap = (Bitmap)Objects.requireNonNull(data.getExtras()).get("data");
        Intent intent = new Intent(MainActivity.this,Camera.class);
        intent.putExtra("Bitmap",bitmap);
        startActivity(intent);
    }
    else if(requestCode == PICK_IMAGE){
        if(data != null){
            imageUri = data.getData();
            Intent intent = new Intent(MainActivity.this,Camera.class);
            intent.putExtra("imageUri",imageUri);
            startActivity(intent);
        }
     }
   }
}

Camera.java

public class Camera extends AppCompatActivity {

Button btnCap, btnUse;
ImageView imageView3;
Uri imageUri;



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

    btnCap = (Button) findViewById(R.id.btnCap);
    btnUse = (Button) findViewById(R.id.btnUse);
    imageView3 = (ImageView) findViewById(R.id.imageView3);

    if (getIntent().getExtras() != null) {
        imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
        imageView3.setImageURI(imageUri);
       }

    Intent intentCam = getIntent();
    Bitmap camera_img = (Bitmap)intentCam.getParcelableExtra("Bitmap");
    if(camera_img != null){
        imageView3.setImageBitmap(camera_img);
     } 
  }
}
Sandeep Insan
  • 348
  • 1
  • 7
LuckyMe
  • 13
  • 5

1 Answers1

1

Try this it is working fine for me.

 //Use  this method to select image from Gallery
 private void processGalleryImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            GALLERY_REQUEST_CODE);
}

//Use  this method to click image from Camera
private void processCameraImage() {
    Intent cameraIntent = new 
    Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}



 //Use  this method to get image from Gallery/Captured from Camera
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_REQUEST_CODE) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");

            //Starting activity (ImageViewActivity in my code) to preview image 
            Intent intent = new Intent(this, ImageViewActivity.class);
            intent.putExtra("BitmapImage", photo);
            startActivity(intent);
        } else if (requestCode == GALLERY_REQUEST_CODE) {
            if (data.getData() != null) {
                Uri imageUri = data.getData();

                //Starting activity (ImageViewActivity in my code) to preview image 
                Intent intent = new Intent(this, ImageViewActivity.class);
                intent.putExtra("ImageUri", imageUri.toString());
                startActivity(intent);
            }
        }
    }
}


 //ImageViewActivity code

 private Bitmap bitmap;
 private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_view);
    ButterKnife.bind(this);


    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        bitmap = bundle.getParcelable("BitmapImage");
        uri = bundle.getString("ImageUri");

        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else
            Glide.with(this).load(uri).into(imageView);
    }
}

I am using the Glide library to load image in image view in case if i have image url.

Sandeep Insan
  • 348
  • 1
  • 7
  • its still have a problem. I changed the process of displaying the image by staying on one activity and not passing it. But my problem now is i cant save it on my gallery even though i tried other codes here in stackoverflow. any help? Thank you! – LuckyMe Nov 20 '18 at 10:09
  • Check it out if still face problem let me know https://stackoverflow.com/questions/6027720/android-how-to-save-captured-image-into-phones-gallery – Sandeep Insan Nov 20 '18 at 10:47
  • done on saving it on gallery but for now i cannot display the captured image on the imageview on the same activity why so?? :( – LuckyMe Nov 20 '18 at 12:39
  • also it doesn't show any error on my logcat. is it possible to do both? save the image then display it on imageview? – LuckyMe Nov 20 '18 at 12:47
  • Hi Sandeep can i send this to your email? so that i can ask for assistance furtheer? thank you? – LuckyMe Nov 24 '18 at 12:06
  • what you want to send in email ? – Sandeep Insan Nov 24 '18 at 14:26