Android摄像机:数据意图返回空我有一个Android应用程序,它包含多个活动。在其中一种情况下,我使用一个按钮来调用设备摄像机:public void onClick(View view) {
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoIntent, IMAGE_CAPTURE);}在相同的活动中,我称之为OnActivityResult方法获取图像结果:@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.pic);
imageview.setImageBitmap(image);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "CANCELED ", Toast.LENGTH_LONG).show();
}
}}问题是data为空,而OnActivityResult方法直接转到(resultCode == RESULT_CANCELED)并且应用程序返回到以前的avtivity。如何解决此问题,并且在调用摄像机之后,应用程序返回到当前活动,其中包含ImageView哪个将包含拍摄的照片?谢谢
3 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
o
private void openCameraForResult(int requestCode){ Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uri = Uri.parse("file:///sdcard/photo.jpg"); photo.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(photo,requestCode);}if (requestCode == CAMERA_REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { File file = new File(Environment.getExternalStorageDirectory().getPath(), "photo.jpg"); Uri uri = Uri.fromFile(file); Bitmap bitmap; try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); bitmap = crupAndScale(bitmap, 300); // if you mind scaling pofileImageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public static Bitmap crupAndScale (Bitmap source,int scale){ int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth(); int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth(); int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2; int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2; source = Bitmap.createBitmap(source, x, y, factor, factor); source = Bitmap.createScaledBitmap(source, scale, scale, false); return source;}
- 3 回答
- 0 关注
- 368 浏览
添加回答
举报
0/150
提交
取消