package
com.shruti.cameraexample;
import
androidx.annotation.Nullable;
import
androidx.appcompat.app.AppCompatActivity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.graphics.drawable.BitmapDrawable;
import
android.net.Uri;
import
android.os.Bundle;
import
android.provider.MediaStore;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageView;
public
class
MainActivity
extends
AppCompatActivity {
private
final
int
CAMERA_REQ_CODE =
100
;
private
final
int
GALLERY_REQ_CODE =
1000
;
ImageView imgCamera;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgCamera=findViewById(R.id.imgCamera);
Button btnCamera = findViewById(R.id.btnCamera);
Button btnGallery = findViewById(R.id.btnGallery);
btnGallery.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent iGallery =
new
Intent(Intent.ACTION_PICK);
iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(iGallery,GALLERY_REQ_CODE);
}
});
btnCamera.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent iCamera =
new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(iCamera,CAMERA_REQ_CODE);
}
});
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
@Nullable
Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
if
(resultCode==RESULT_OK){
if
(requestCode==GALLERY_REQ_CODE){
imgCamera.setImageURI(data.getData());
}
else
if
(requestCode==CAMERA_REQ_CODE) {
Bitmap img = (Bitmap)(data.getExtras().get(
"data"
));
imgCamera.setImageBitmap(img);
}
}
imgCamera.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imgCamera.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
String bitmpath = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,
"upload"
,
null
);
Uri uri = Uri.parse(bitmpath);
Intent shareIntent =
new
Intent(Intent.ACTION_SEND);
shareIntent.setType(
"image/png"
);
shareIntent.setPackage(
"com.whatsapp"
);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(shareIntent,
"Share Using"
));
}
});
}
}