user2021731
user2021731

Reputation: 53

IMAGE_CAPTURE on resultActivity the file doesn't exist yet

This a rare case, i'm trying to capture an image from native camera activity on my samsung galaxy s3 and onActivityResult the file doesn't exist to read yet. If i disconnect the phone and connect again then the i can see the image on my explorer.

It likes that the camera activity writes the file with delay or something...

i have readed all questions and answers and other forums, but it is my problem; My code:

... onCreate
      File file = new File( _path );
      pathUri = Uri.fromFile( file );
...

  protected void startCameraActivity()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, pathUri);
        startActivityForResult( intent, CAMERA_REQUEST_CODE );
    }    

  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
        {
        // test if exits image
        if((new File(_path)).exists())
            ... do something
        }
    ... continue

if((new File(_path)).exists()) always retunrs FALSE, how is it possible?

Thans in advance

Upvotes: 2

Views: 1113

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

I have never seen this occur in my app. However, this could potentially be a workaround.

Note: Declare the Uri instance initialURI globally so you can use it throughout the Activity. You could also change the name to something that suits your naming convention.

In this, right before the Intent is triggered, I pass a specific path for the Image to be stored at along with the name for the File.

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals
        (android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
            "SOME_FOLDER_NAME/");
else
    cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";

File photo = new File(Environment.getExternalStorageDirectory(), 
        "SOME_FOLDER_NAME/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);

startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);

And finally, in the onActivityResult():

Note: This I'm guessing should work. My application uses the Aviary SDK and the code for that differs vastly from the conventional work done in this method.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {

        // USE EITHER THIS
        imgView.setImageURI(initialURI);

        // OR USE THIS
        getContentResolver().notifyChange(initialURI, null);

        ContentResolver cr = getContentResolver();

        try {   
            // SET THE IMAGE FROM THE CAMERA TO THE IMAGEVIEW
            Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, initialURI);

            // SET THE IMAGE USING THE BITMAP
            imgvwSelectedImage.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

If you do not need the the Image stored on the SD-Card, you could delete it once you are done with it. This I am guessing (guessing because I have never experienced what the OP is facing problem with) this should do it for you.

Oh. Almost forgot. You will need to declare this permission in your Manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 2

Related Questions