我正在开发一个应用程序,它将允许用户使用相机获取图片或从图库中选择并将该图像传递给要显示的其他活动。我照顾好了相机部分,但从图库中挑选图像却强制关闭了该应用程序。
这是这段代码: 主要:
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){
Intent intent = new Intent(MainActivity.this,Camera.class);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
}
else if(requestCode == PICK_IMAGE){
imageUri = data.getData();
try{
Bitmap bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Intent intent2 = new Intent(MainActivity.this,Camera.class);
intent2.putExtra("Bitmap",bitmap2);
startActivity(intent2);
}catch(IOException e){
e.printStackTrace();
}
}
}
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);
//camera
Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("Bitmap");
imageView3.setImageBitmap(bitmap);
}
}
这是我使用图库中的图像时遇到的错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.miui.gallery.open/raw//storage/emulated/0/DCIM/Camera/IMG_20181117_043417.jpg typ=image/jpeg flg=0x1 }} to activity {com.example.janrex.readem/com.example.janrex.readem.MainActivity}: java.lang.RuntimeException: Failure from system