gpt4 book ai didi

android - 三星 S3 原生相机的奇怪问题导致结果崩溃

转载 作者:行者123 更新时间:2023-11-29 21:12:52 28 4
gpt4 key购买 nike

我的应用程序具有以下代码来调用 native 相机的捕获; startActivityForResult

它已经在 nexus 5、HTC 1、nexus 7、Samsung S4 和 Samsung S3 上进行了测试。它适用于除 S3 以外的所有设备。在 S3 上,应用程序在返回开始 Activity 时崩溃

崩溃:

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());
}
}
}

}

最佳答案

所以问题似乎是,无论出于何种原因,三星 S3 的原生相机在设置结果时都没有返回数据。因此,当您获得适当的结果代码时,没有实际数据被传回。为了在我的结果监听器中解决这个问题,我检查数据是否为空。数据假设是所拍照片的文件路径。如果路径为空 && 结果代码为 RESULT_OK,我只是通过我告诉相机保存图片的目录并获取最后创建的文件的路径。

    if(heroFilePath==null){
File dir = new File(g.kPhotoDirectory);

File[] files = dir.listFiles();

File lastModifiedFile = files[0];

for (int i = 1; i < files.length; i++) {
if (lastModifiedFile.lastModified() < files[i].lastModified()) {
lastModifiedFile = files[i];
}
}
heroFilePath = lastModifiedFile.getAbsolutePath();
}

关于android - 三星 S3 原生相机的奇怪问题导致结果崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256613/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com