我的应用程序具有以下代码来调用本机相机的捕获;startActivityForResult
它已经在 nexus 5、HTC 1、nexus 7、三星 S4 和三星 S3 上进行了测试。它适用于除 S3 之外的所有设备。在 S3 上,应用程序在返回开始活动时崩溃
迷恋;撞车;崩溃:
03-07 13:09:21.297: E/ActivityThread(6535): Activity com.DRPMapViewActivity
has leaked ServiceConnection android.media.MediaScannerConnection@42bd73d8 that
was originally bound here
03-07 13:09:21.297: E/ActivityThread(6535): android.app.ServiceConnectionLeaked:
Activity com.DRPMapViewActivity has leaked ServiceConnection
android.media.MediaScannerConnection@42bd73d8 that was originally
bound here
我的代码
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(g.kPhotoDirectory);
// File storageDir = new
// File(Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_DCIM), "Drop");
storageDir.mkdirs();
File image = File.createTempFile(imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFileFromCapture = null;
try {
photoFileFromCapture = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFileFromCapture != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFileFromCapture));
startActivityForResult(takePictureIntent,
g.kRequestImageCaptureCode);
}
}
}
private void dispatchChoosePictureIntent() {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
// Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
startActivityForResult(i, g.kRequestImageChooserCode);
}
我的 onActivityResult 看起来像这样:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == g.kGenericRequestCode) {
if (resultCode == g.kKillMeResultCode) {
finish();
}
Log.v("activityResult", "requestcode:" + requestCode
+ " resultCode:" + resultCode + " data:" + data);
super.onActivityResult(requestCode, resultCode, data);
}
if (requestCode == g.kRequestImageChooserCode
&& resultCode == Activity.RESULT_OK) {
Uri imageUri = data.getData();
Log.v("CAPTURE", "uri:" + imageUri);
String filePath = getRealPathFromURI(imageUri);
Intent i = new Intent(DRPMapViewActivity.this,
DRPCreateDropActivity.class);
i.putExtra("USER", _user);
i.putExtra("LATLNG", getLocationForCreateDrop());
i.putExtra("FILE_PATH", filePath);
i.putExtra("TYPE", CreateDropType.kImageFile);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
}
if (requestCode == g.kRequestImageCaptureCode) {
Log.v("CAPTURE RESULT", "result:" + resultCode);
if(resultCode== Activity.RESULT_OK){
MediaScannerConnection.scanFile(this,
new String[] { mCurrentPhotoPath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
Intent i = new Intent(DRPMapViewActivity.this,
DRPCreateDropActivity.class);
i.putExtra("USER", _user);
i.putExtra("LATLNG", getLocationForCreateDrop());
i.putExtra("FILE_PATH", mCurrentPhotoPath);
i.putExtra("TYPE", CreateDropType.kImageFile);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
if (data != null) {
Log.v("CAPTURE RESULT", "data:" + data.getData());
}
}
}
}