Joseph Dailey
Joseph Dailey

Reputation: 4915

Stock Camera Intent resulting as RESULT_OK but not actually saving the file

I had this working a couple weeks ago and now I am completely baffled. Everything works swell except the file is never actually created.

public void takePic(){
    File photoPath = new File(getFilesDir(), "temp_img.jpg");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoPath));
    startActivityForResult(intent, PHOTO);
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK){
        if(requestCode == PHOTO){
            //stuff with file
        }
    }
}


//manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Every thing goes swell until I use the file later and it turns out it doesn't exist, therefore getting a "file not found."

Upvotes: 0

Views: 410

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199805

getFilesDir() returns a directory that only your application can access, hence the Camera app is unable to save the file. Replace getFilesDir() with getExternalFilesDir(Environment.DIRECTORY_PICTURES) which all apps can read/write to.

Upvotes: 2

Related Questions