2

I am following Google's Taking Photos Simply tutorial, but it is failing on the temp file creation with an java.io.IOException: open failed: EACCES (Permission denied).

Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="pricez.fastshop_android" >
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
    <uses-feature android:required="true" android:name="android.hardware.camera"/>
    <!-- Rest of file... -->
</manifest>

So, I guess that at least this is correct.

Here is the relevant code of my activity:

private String mCurrentPhotoPath;
private static final int REQUEST_TAKE_PHOTO = 1;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "/JPEG_" + timeStamp + "_"; // I forgot from where I got this, but it was from some SO question that tried to deal with a similar problem
    File absoluteFileDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsoluteFile(); // Same as above
    Log.i("createImageFile", "absoluteFileDir.getAbsolutePath(): " + absoluteFileDir.getAbsolutePath()); // I/createImageFile﹕ absoluteFileDir.getAbsolutePath(): /storage/sdcard/Android/data/-redacted-/files/Pictures
    File image = File.createTempFile(imageFileName, ".jpg", absoluteFileDir);
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

public void takePhoto(View view) {
    if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
        return;
    }
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile;
        try {
            photoFile = createImageFile();
        } catch (IOException e) {
            Log.e("takePhoto", "IOException", e);
            return;
        }
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        // Get the dimensions of the View
        ImageView imgv = (ImageView)findViewById(R.id.imgPreview);
        int targetW = imgv.getWidth();
        int targetH = imgv.getHeight();
        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;
        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;
        //
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        imgv.setImageBitmap(bitmap);
    }
}

EDIT 1: I'm running this on a emulator, with SDK level 19.

EDIT 2:

If I delete the folders by hand, the getExternalStorageDirectory starts working fine. I'm leaving this question open because it's not really a fix. If everything keeps working in my code, I'm going to close in a few days.

CatarinaPBressan
  • 132
  • 2
  • 13
  • What version of Android are you testing this on? Also, is it an emulator or a real device? – user959631 Aug 13 '14 at 17:09
  • have a look here: http://stackoverflow.com/questions/3660572/android-createtempfile-throws-permission-denied – ligi Aug 13 '14 at 17:52
  • Both methods in that answer "worked" in the sense that it isn't throwing errors anymore. However, when I click on `Done` in the camera app, `onActivityResult` isn't being called. If I click in the `Cancel` button, the camera app exits and calls the `onActivityResult` method. The file created is corrupted or otherwise unable to be read on my machine when I pull them with adb. – CatarinaPBressan Aug 13 '14 at 18:14
  • 1
    Does absoluteFileDir exist? If not use mkdirs on it. – greenapps Aug 13 '14 at 18:31
  • I deleted the Picture folder via adb to make sure that it didn't need to be recreated (I had noticed earlier that it created itself), the code started working fine, folder and all. – CatarinaPBressan Aug 13 '14 at 20:05

1 Answers1

1

I have no idea why (my guess is that something in the file locks has silently gone horribly wrong), but it started working when I deleted and recreated the folder (Kudos to @greenapps, would upvote if I could). What I can do at this point is to think how to recover somehow.

I don't think deleting the folder is the actual solution, but I have to move on at this point.

Plus, the situation might not present itself in an actual device.

CatarinaPBressan
  • 132
  • 2
  • 13