10

I follow this tutorial to take photo and save into device storage, but when I execute it, it got an exception

java.io.IOException: Permission denied 
 on line:

File image = File.createTempFile(imageFileName, ".jpg", storageDir);

How to solve this?

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(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

--Edit--
Here is my manifests

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.idea.takephoto">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

1
  • 1
    please declare this permission in manfest : android.permission.WRITE_EXTERNAL_STORAGE Commented Dec 9, 2016 at 4:43

7 Answers 7

10

As in the Android Documentation, you need to write to the external storage, you must request the WRITE_EXTERNAL_STORAGE permission in your manifest file:

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

If you use API 23 (Marshmallow) and above, you need to Requesting Permissions at Run Time because it's a Dangerous Permission.

Sign up to request clarification or add additional context in comments.

2 Comments

All my problem is need to request the permission. Thank you
Thanks! This fixed my issue
9

If your target api level is >=29 or you are using android 10 then please add following line in your app's manifest file in application tag

android:requestLegacyExternalStorage="true"

Comments

3

Put permission in manifest file:

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

and add createTempFile code in try catch block:

    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File file = null;
    try {
        file = File.createTempFile(timeStamp, ".jpg", storageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mCurrentPhotoPath = String.valueOf(Uri.fromFile(file));
    return file;

Comments

2

add permission in your manifest file

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

if its above API 23 Storage permission will do like this

Comments

2

I had the same issue when using a device of android 10 api 29. I could solve it by replacing Environment.getExternalStoragePublicDirectory with context.getExternalFilesDir.

Comments

0

Write this tag in your project's manifest file, and give permission to your app from setting in your android phone before you are going to run it.

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

Comments

0

also add this in application field manifesto android:requestLegacyExternalStorage="true">

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.