04-22-2012 05:48 PM
Now for a question that is not so stupid. I hope.
I finally have my android app converted, installed and running. But it doesn't seem to be able to find files in /sdcard. I placed the file into the misc/android folder on the playbook but my app doesn't seem to be able to find it. I am getting errors when I try to open the file.
Solved! Go to Solution.
04-23-2012 09:55 AM
More information. I am getting "permission denied" errors. Is there something that I have to do to enable access the the /sdcard folder from an android app?
04-23-2012 10:53 AM
Can you post your code where you are trying to read data from the SD card? Mainly the part where you specify the path to the file on the SD Card?
04-23-2012 11:19 AM
Here is the code that I am using:
// Check if the SD card is present String sdState = Environment.getExternalStorageState(); if (! Environment.MEDIA_MOUNTED.equals(sdState)) { AlertDialog dlg = new AlertDialog.Builder(FActivity).create(); dlg.setTitle("Error"); dlg.setMessage("There is no SD card in this device"); dlg.setButton("OK", FDialogClickHandler); dlg.show(); } // Get the path to the SD card's root directory File sdRoot = Environment.getExternalStorageDirectory(); try { // Open the file input stream. To do this we need to open a // plain vanilla file stream and the a LineNumberStream which // allows us to read the file line-by-line. FileReader frdr = new FileReader(new File(sdRoot, "BookList.txt")); LineNumberReader lrdr = new LineNumberReader(frdr); lrdr.setLineNumber(0); FBooks.clear(); String line; // Read all lines in the file while ((line = lrdr.readLine()) != null) { // Parse the quoted, comma delimited text into // separate fields and add the book to our list // of books. ArrayList<String> flds = ParseBook(line); FBooks.add(new Book(flds.get(0), flds.get(1), flds.get(2))); } } catch (FileNotFoundException E) { // I AM CATCHING AN EXCEPTION HERE AlertDialog dlg = new AlertDialog.Builder(FActivity).create(); dlg.setTitle("Error"); dlg.setMessage("There is no BookList.txt on the SD card in this device (" + E.getMessage() + ")"); dlg.setButton("OK", FDialogClickHandler); dlg.show(); }
04-23-2012 11:36 AM - edited 04-23-2012 11:39 AM
Unfortunately, I don't have a concrete answer for you. But, I do have some ideas...
1) Are you certain what Environment.getExternalStorageDirectory() is returning? You might want to spit out the path of the 'sdRoot' variable to see what it contains.
2) On another forum, someone had success reading and they used this line (below). The only difference I notice is the getPath() on the end of getExternalStorageDirectory(). I'm not sure if that would make a difference, but if #1 isn't what you expect, you can always try this method.
File sdcardFile =newFile(Environment.getExternalStorageDirectory().getPath()+"/tmp.txt");
3) I saw another post that seemed to focus on writing to external storage, but it has quite a bit of information on mapping folders correctly and how to get access to various different areas of playbook storage. What caught my eye were the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_ST
<uses-permission android:name="android.permission.WRITE_MEDIA_STORA
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FIL
Anyways, it's worth a look...
Hopefully the answer is somehwere in here. Unfortunately, I'm at work right now, so I can't do any testing myself. ![]()
Mo
04-23-2012 04:01 PM
Putting these extra permission into my android manifest seems to have solved the problem.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_MEDIA_STORA GE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FIL ESYSTEMS" />
Not sure why, but you can't argue with success.
Thank you for your help.
04-24-2012 11:06 AM
04-24-2012 11:46 AM
Yep, that was it. All that was needed was the MOUNT_UNMOUNT_FILESYSTEMS permission.
11-04-2012 11:30 AM
thanks guys, this issue really drove me crazy. had spent many hours before getting here.
but both MOUNT_UNMOUNT_FILESYSTEMS and WRITE_EXTERNAL_STORAGE are required in my case, readonly tho. WRITE_MEDIA_STORAGE is not shown in Eclipse manifest editor any more.
a month ago