我发布了一个应用程序,其中一项基本功能是允许用户拍照,然后将该照片保存在其外部存储上的特定文件夹中。
一切似乎都运行良好,但我现在收到两份报告,声称在拍照后单击“完成”退出相机(并返回到 Activity),应用程序被强制关闭,将用户带回主屏幕。
这发生在三星 Nexus S 和 Galaxy Tab 上。下面我发布了我的代码以显示我设置了我的 Intent 以及我如何处理在 onActivityResult() 中保存和显示照片。在单击“完成”退出相机应用程序后可能导致其崩溃的任何指导,将不胜感激!
同样,这似乎在大多数设备上运行良好,但我想知道它们是否是我应该采用的更有效、更通用的方法。谢谢
我是如何触发相机 Intent 的
case ACTION_BAR_CAMERA:
// numbered image name
fileName = "image_" + String.valueOf(numImages) + ".jpg";
output = new File(direct + File.separator + fileName); // create
// output
while (output.exists()) { // while the file exists
numImages++; // increment number of images
fileName = "image_" + String.valueOf(numImages) + ".jpg";
output = new File(outputFolder, fileName);
}
camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
uriSavedImage = Uri.fromFile(output); // get Uri of the output
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); //pass in Uri to camera intent
startActivityForResult(camera, 1);
phá vỡ;
mặc định:
return super.onHandleActionBarItemClick(item, position);
}
trả về giá trị đúng;
}
我如何设置 onActivityResult()
@Ghi đè
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Phương thức tự động tạo stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // If data was passed successfully
Bundle extras = data.getExtras();
//Bundle extras = data.getBundleExtra(MediaStore.EXTRA_OUTPUT);
/*ad = new AlertDialog.Builder(this).create();
ad.setIcon(android.R.drawable.ic_menu_camera);
ad.setTitle("Save Image");
ad.setMessage("Save This Image To Album?");
ad.setButton("Ok", this);
ad.show();*/
bmp = (Bitmap) extras.get("data"); // Set the bitmap to the bundle
// of data that was just
// received
image.setImageBitmap(bmp); // Set imageview to image that was
// captured
image.setScaleType(ScaleType.FIT_XY);
}
}
首先让我们明确一点 - 我们有两个选项可以从相机中获取 onActivityResult 中的图像数据:
1. 通过传递您要保存的图像的确切位置 Uri 来启动相机。
2. Just Start Camera 不传递任何 Loaction Uri。
1 .第一种情况:
通过将图像 Uri 传递到您要保存的位置来启动相机:
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(it, CAMERA_RESULT);
在 onActivityResult 中接收图像为:
@Ghi đè
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
iv = (ImageView) findViewById(R.id.ReturnedImageView);
// Decode it for real
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = sai;
//imageFilePath image path which you pass with intent
Bản đồ bit bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
// Display it
iv.setImageBitmap(bmp);
}
}
2 。第二种情况:
如果您想在 Intent(data) 中接收图像,则在不传递图像 Uri 的情况下启动相机:
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it, CAMERA_RESULT);
在 onActivityResult 接收图像为:
@Ghi đè
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
// Get Extra from the intent
Bundle extras = data.getExtras();
// Get the returned image from extra
Bitmap bmp = (Bitmap) extras.get("data");
iv = (ImageView) findViewById(R.id.ReturnedImageView);
iv.setImageBitmap(bmp);
}
}
*****编码愉快!!!!*****
Tôi là một lập trình viên xuất sắc, rất giỏi!