So I am trying to take a pic with camera and pass it to another activity through intent to be shown in ImageView. Relevant code -
cameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
}
else
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_CAMERA_PERMISSION_CODE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
else
{
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, CamCapture.class); //The problem seems to be here somewhere
intent.putExtra("picture", byteArray);
startActivity(intent);
}
}
I above code when I click cameraBtn icon in my Activity it has onclick listener which provides required permission to click image. After permission is granted camera opens. I click the image but after that the default ok button OR TICK button like this does not appear and image just gets saved -
If you may know that to go to next activity where i have ImageView to show captured Image it is necessary that i click on OK or TICK but the code just doesn't follow the required procedure.
Here is the CamCapture class code as requested by Dat Pham -
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = findViewById(R.id.camImg);
image.setImageBitmap(bmp);
If your code crashes at Intent intent = new Intent(this, CamCapture.class);
as you mentioned in your question, I suspect missing declaration on the CamCapture
activity. Make sure that:
CamCapture
activity is publicCamCapture
activity is declared in the AndroidManifest.xml
I created a demo that uses your code, and it works correctly for me. You can find the demo on github
How the code works on my device: https://i.sstatic.net/7HZOg.jpg