0

i want to write file on external storage but it gives me error:

java.io.FileNotFoundException: /mnt/media_rw/sdcard1/Backup/Contact/18-03-2016 16:03:17.vcf: open failed: EACCES (Permission denied)

My code :

 fileContact = new File("/mnt/media_rw/sdcard1/", "Backup/Contact");
                            if (!fileContact.exists()) {
                                fileContact.mkdirs();
                            }

                            Calendar c = Calendar.getInstance();
                            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
                            String strDate = sdf.format(c.getTime());

                            StoragePath = fileContact.getAbsolutePath() + "/" + strDate + ".vcf";

When i write fileContact = new File("/sdcard/", "/Backup/Contact"); then it write file in internal storage. i want to write this file on my memory card(external sdcard) .

I am getting all mounted/external storage list from this code - http://stackoverflow.com/a/19982338/3774906 and it gives me perfect list.

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.xxx.xx"
    xmlns:android="http://schemas.android.com/apk/res/android">



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



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

    <uses-feature android:name="android.hardware.usb.host"/>

    <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"
            android:autoRemoveFromRecents="true"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        </activity>


    </application>

</manifest>

So how can i write this file on external storage card ?

  • please show Your complete manifest.... – Opiatefuchs Mar 18 '16 at 11:40
  • @Opiatefuchs Added my full manifest. –  Mar 18 '16 at 11:42
  • and don´t hardcode the path. Environment.getExternalStorageDirectory().getAbsolutePath() will give You the path to the sdcard. But be aware of that some manufacturers even lead You to a virtuell sd card. Read about the path on docs: http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29 – Opiatefuchs Mar 18 '16 at 11:44
  • but Environment.getExternalStorageDirectory().getAbsolutePath() gives me internal storage path only. –  Mar 18 '16 at 11:45
  • that´s what I meant with it´s manufacturer problem. Some does it on this, some on the other way. You must keep this in mind if You want to populate an app for different devices. Try it with getExternalFilesDir(String)...it´s too much to explain it here exactly so please read the docs I linked in my comment above... – Opiatefuchs Mar 18 '16 at 11:48
  • Ok. let me read this doc. –  Mar 18 '16 at 11:51
  • 1
    Your hard-coded path is not [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) on any device that I have ever encountered. For some devices, that might be [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). You do not have arbitrary access to removable storage on Android 4.4+. – CommonsWare Mar 18 '16 at 12:29

1 Answers1

0

Your below line for access external storage is seems problematic

 fileContact = new File("/mnt/media_rw/sdcard1/", "Backup/Contact");

Some device manufacturer not use sdcard1

Always follow this way for accessing external storage

 String state = Environment.getExternalStorageState();

 File root = android.os.Environment.getExternalStorageDirectory(); 
Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
  • state will give me external storage state? –  Mar 18 '16 at 11:52
  • i have read this and i am get the state mounted also. even i have also write `fileContact = new File(getExternalFilesDir(null), "MagicSpy/Backup/Contact");` but its not create file on mounted external sd card –  Mar 18 '16 at 12:34