-1

My android app on Android-M have the permission declared in Manifest. android.permission.WRITE_EXTERNAL_STORAGE

My app starts media player which sends control to Jni layer and then to MediaPlayerService and finally to NuPlayer. I understand that my app and NuPlayer are running in two seperate process (http://rxwen.blogspot.in/2010/01/understanding-android-media-framework.html).

Now if i try to create a file in sdcard from native process, suppose from (NuPlayer.cpp) : FILE *file = fopen("/storage/emulated/0/Test/test.txt", "w+");

file is coming as null and errno is 13 (No permission). So need to know how to give permission to native process to create file on sdcard on Android M.

Thanks in advance for the help.

  • How do you obtain the filepath in Android and how you pass it to the native code "/storage/emulated/0/Test/test.txt"? – Radu Ionescu Jan 12 '16 at 12:26
  • In android M you can't assume that user accept the permission. So you need to check whether permission granted or not? If not granted by user then ask them to grant (in my Pop up will generate) and do whatever you want. –  Jan 12 '16 at 12:29
  • Try my code. It will help you. –  Jan 12 '16 at 12:30
  • https://stackoverflow.com/questions/32635704/cant-get-the-permission – CommonsWare Jan 12 '16 at 12:32

1 Answers1

0

In Marshmallow you need to ask for permission to user to achieve this try,

1.in your manifest add,

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

2.Create function for write into external storage.

 private void createFileExternalStorage() {
        MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
        if (!marshMallowPermission.checkPermissionForExternalStorage())
            marshMallowPermission.requestPermissionForExternalStorage();

        File backupFile;
        File appFolder;
        // if SDCard available
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
            appFolder = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.app_name));
            if (!appFolder.exists())
                appFolder.mkdir();
            Log.d(TAG,"In External");
        }else {   // if not SDCard available
            ContextWrapper cw = new ContextWrapper(this);
            appFolder = cw.getDir(getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
            if (!appFolder.exists())
                appFolder.mkdir();
            Log.d(TAG,"In internal");
        }
        //create a new file, to save the downloaded file
        backupFile = new File(appFolder, "backup.txt");
        Log.d(TAG, "file @" + backupFile.getAbsolutePath());

        try {
            FileOutputStream f = new FileOutputStream(backupFile);
            PrintWriter pw = new PrintWriter(f);
            pw.println("Hi , How are you");
            pw.println("Hello");
            pw.flush();
            pw.close();
            f.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.i(TAG, "******* File not found. Did you" +
                    " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

3.Create class MarshMallowPermission to get & ask for permission.

public class MarshMallowPermission {
    public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
    Activity activity;

    public MarshMallowPermission(Activity activity) {
        this.activity = activity;
    }


    public boolean checkPermissionForExternalStorage(){
        int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    }

    public void requestPermissionForExternalStorage(){
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Toast.makeText(activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

}
Marlon
  • 1,839
  • 2
  • 19
  • 42