I need to accomplish these steps:
- Let the user select a file ( any kind of file )
- Get a permanent reference to access the file later or copy to my internal storage.
The first one is really a easy. I don't have any problems in fetch the file using Intent.ACTION_GET_CONTENT. I also can get the MimeType of the file. But I can't copy.
I tried to copy using a FileInputStream, but apparently I don't have the permission to it. I really hope that somebody can help me.
Permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Intent Call
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
if ( intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_FILE_GET);
}
Failed attempt to copy the file
FileInputStream in = (FileInputStream) getContentResolver().openInputStream( data.getData());
FileOutputStream out = new FileOutputStream( "copy.jpg ");
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
in.close();
out.close();
Error: android.system.ErrnoException: open failed: EROFS (Read-only file system)
You are attempting to create the output stream file in the root directory,
/copy.jpg
, which is read-only.Use one of the Environment methods, such as
getExternalStorageDirectory()
, or one of the Context methods such asgetFilesDir()
, to build a path to a directory you have write access to.For example:
This documentation describes the different options for storing files in internal or external storage.