如何解决单击图片后,图像捕获的 onActivityResult 不显示确定按钮
所以我试图用相机拍照并通过意图将其传递给另一个活动以在 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 按钮不会出现,图片只是被保存 -
如果您可能知道要转到下一个我有 ImageView 显示捕获图像的活动,则必须单击“确定”或“勾选”,但代码并未遵循所需的程序。
这是 Dat Pham 要求的 CamCapture 类代码 -
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,byteArray.length);
ImageView image = findViewById(R.id.camImg);
image.setimageBitmap(bmp);
解决方法
如果您的代码在问题中提到的 Intent intent = new Intent(this,CamCapture.class);
崩溃,我怀疑 CamCapture
活动缺少声明。确保:
-
CamCapture
活动是公开的 -
CamCapture
活动在AndroidManifest.xml
中声明
我创建了一个使用您的代码的演示,它对我来说工作正常。您可以在 github
上找到演示代码如何在我的设备上运行:https://imgur.com/a/9XmGMD6
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。