单击图片后图像捕获上的 onActivityResult 不显示确定按钮
onActivityResult on Image capture does not show OK button after clicking picture
所以我想用相机拍张照片,然后通过 intent 将其传递给另一个 activity,以便在 ImageView 中显示。相关代码-
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);
}
}
当我在 Activity 中单击 cameraBtn 图标时,我上面的代码有 onclick 侦听器,它提供单击图像所需的权限。获得许可后,相机打开。我点击了图片,但之后默认的 ok 按钮或 TICK 按钮不会出现,图片会被保存 -
如果您可能知道要转到下一个 activity 那里我有 ImageView 来显示捕获的图像,则我有必要单击“确定”或“勾选”,但代码只是不遵循所需的程序。
这是 Dat Pham 要求的 CamCapture class 代码 -
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);
如果您的代码如您在问题中提到的那样在 Intent intent = new Intent(this, CamCapture.class);
处崩溃,我怀疑缺少 CamCapture
activity 上的声明。确保:
CamCapture
activity 是 public
CamCapture
activity 在 AndroidManifest.xml
中声明
我创建了一个使用您的代码的演示,它对我来说工作正常。您可以在 github
上找到演示
代码在我的设备上的工作方式:https://imgur.com/a/9XmGMD6
所以我想用相机拍张照片,然后通过 intent 将其传递给另一个 activity,以便在 ImageView 中显示。相关代码-
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);
}
}
当我在 Activity 中单击 cameraBtn 图标时,我上面的代码有 onclick 侦听器,它提供单击图像所需的权限。获得许可后,相机打开。我点击了图片,但之后默认的 ok 按钮或 TICK 按钮不会出现,图片会被保存 -
如果您可能知道要转到下一个 activity 那里我有 ImageView 来显示捕获的图像,则我有必要单击“确定”或“勾选”,但代码只是不遵循所需的程序。
这是 Dat Pham 要求的 CamCapture class 代码 -
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);
如果您的代码如您在问题中提到的那样在 Intent intent = new Intent(this, CamCapture.class);
处崩溃,我怀疑缺少 CamCapture
activity 上的声明。确保:
CamCapture
activity 是 publicCamCapture
activity 在AndroidManifest.xml
中声明
我创建了一个使用您的代码的演示,它对我来说工作正常。您可以在 github
上找到演示代码在我的设备上的工作方式:https://imgur.com/a/9XmGMD6