我有一个应用程序,我可以在其中打开相机并拍照并保存图库。当我点击按钮打开相机时,它的工作原理。如果我在没有拍照的情况下点击手机上的后退按钮,我的应用程序就会停止。我怎么解决这个问题 ?
Button camera = (Button) findViewById(R.id.camer);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_SEND);
//Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + " " + pic.exists());
if (pic != null) {
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
}
i.setType("image/jpg");
startActivity(Intent.createChooser(i, "Share you on the jobing"));
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
image = (ImageView) findViewById(R.id.imageToUpload);
image.setImageBitmap(thumbnail);
try {
File root = Environment.getDataDirectory();
if (root.canWrite()) {
pic = new File(root, "pic.jpg");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
}
引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()”
因为你data
是空的
试试这个方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_PIC_REQUEST){
if (resultCode == RESULT_OK) {
thumbnail = (Bitmap) data.getExtras().get("data");
image = (ImageView) findViewById(R.id.imageToUpload);
image.setImageBitmap(thumbnail);
try {
File root = Environment.getDataDirectory();
if (root.canWrite()) {
pic = new File(root, "pic.jpg");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
}
}
更多参考:从活动中获取结果
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句