Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Android™ Runtime Development

Reply
New Contributor
sboydlns
Posts: 7
Registered: ‎04-22-2012
My Device: playbook
Accepted Solution

Android app can't find file in /sdcard

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.

 

 

New Contributor
sboydlns
Posts: 7
Registered: ‎04-22-2012
My Device: playbook

Re: Android app can't find file in /sdcard

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?

Contributor
Gophermofur
Posts: 14
Registered: ‎03-16-2012
My Device: Playbook

Re: Android app can't find file in /sdcard

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?

 

 

New Contributor
sboydlns
Posts: 7
Registered: ‎04-22-2012
My Device: playbook

Re: Android app can't find file in /sdcard

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(); }

 

Contributor
Gophermofur
Posts: 14
Registered: ‎03-16-2012
My Device: Playbook

Re: Android app can't find file in /sdcard

[ Edited ]

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_STORAGE" />

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

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

 

Anyways, it's worth a look...

 

http://supportforums.blackberry.com/t5/Android-Runtime-Development/How-to-save-files-to-shared-direc...

 

Hopefully the answer is somehwere in here. Unfortunately, I'm at work right now, so I can't do any testing myself.  :smileyhappy:

 

Mo

 

New Contributor
sboydlns
Posts: 7
Registered: ‎04-22-2012
My Device: playbook

Re: Android app can't find file in /sdcard

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_STORAGE" />
     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

 Not sure why, but you can't argue with success.

 

Thank you for your help.

Contributor
Gophermofur
Posts: 14
Registered: ‎03-16-2012
My Device: Playbook

Re: Android app can't find file in /sdcard

I'd guess it's the MOUNT_UNMOUNT_FILESYSTEMS, but if you get a moment, try removing the other two and see if it still works. It would be nice to know definitively what permissions are required for a SDCARD read operation.
New Contributor
sboydlns
Posts: 7
Registered: ‎04-22-2012
My Device: playbook

Re: Android app can't find file in /sdcard

Yep, that was it.  All that was needed was the MOUNT_UNMOUNT_FILESYSTEMS permission.

New Member
zhutony
Posts: 1
Registered: ‎12-09-2011
My Device: playbook

Re: Android app can't find file in /sdcard

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.

New Member
leobb10
Posts: 1
Registered: ‎01-31-2013
My Device: curve 8350

Re: Android app can't find file in /sdcard

I have the same issue with documents to go that I side loaded to my z10 because the native app has no ability to view or insert footnotes or comments, but the side loaded android app can't access any files from my device or media card.