1

我的应用程序具有以下代码来调用本机相机的捕获;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());
                }
                }
            }

        }
4

1 回答 1

2

所以问题似乎是三星S3的原生相机在设置结果时没有返回数据。因此,当您获得适当的结果代码时,没有实际数据被传回。为了在我的结果监听器中解决这个问题,我检查数据是否为空。数据假定为拍摄照片的文件路径。如果路径为 null && 结果代码为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();
   }
于 2014-03-12T15:14:10.343 回答