Question

My app has the following code to invoke a capture from the native camera; startActivityForResult

It has been tested on a nexus 5, HTC 1, nexus 7, Samsung S4, and Samsung S3. It works great on every device except, the S3. On the S3 the app crashed on return to the starting activity

the crash:

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

my code

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

my onActivityResult looks like this:

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

        }
Was it helpful?

Solution

So the problem seems to be that for what ever reason the Samsung S3's native camera is not returning Data when setting result. So while you get the appropriate result code there is no actual data being passed back. To fix this in my result listener, i check to see if the data is null. The data is suppose to be the file path of the photo taken. If the path is null && the result code is RESULT_OK, I just reiterate through the directory that I told the camera to save the picture in and get the path of the last file created.:

    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();
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top