This is my noob question for the week. I'm looking more for general speculation than specific code and maybe hoping the Android folks are watching and could correct this:
the SDK documentation for Context.openFileOutput says:
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
Ok, that sounds good. I can create a file. Except this method also throws a FileNotFoundException, so apparently something is amiss. Why would a function that is supposed to create a file if it's not found throw an exception if the file is not found???
Kinda defeats that whole "Creates the file..." thing, doesn't it?
I have to apologize for leaping before I looked on this one. I kinda panicked while reading the documentation. After some testing, I found that openFileOutput() does, in fact, work as advertised and will create a file if it's not found, not just throw an FnF exception as I feared. Apparently, the FnF throw was added in case the Activity's application directory does not exist.
Again, my apologies but hopefully, this might help others who are confused by the documentation.
FileNotFoundException is an exception thrown in case that you are trying to write to a file that does not exist, or cannot be currently accessed. When else would this occur?
Perhaps you forgot to close the file, and tried to open the same file.
Perhaps you tried to create multiple FileOutputStream objects pointing to the same file.
These will result in a FileNotFoundException.
Anyway, you can insert a throws FileNotFoundException at the end of your function declaration where you call openFileOutput (and to other functions that call this function).
Possibly also thrown if you use MODE_APPEND, which appends to an existing file and the file doesn't exist.
Have you inserted the right Permission?
see http://developer.android.com/reference/android/Manifest.permission.html
for reference.
i think
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
could be the one You are fine with
Related
I have an app that needs to create a file in an app-specific directory, but that file need to be public so it can be edited by another app, so I need a path like this "/storage/emulated/0/Android/data/com.my.app/files"
I already checked Xamarin Documentation and I found what I need. Based on that Documentation I can use this method Android.Content.Context.GetExternalFilesDir(string type).
But I get this error when I call that method.
An object reference is required for the non-static field, method, or property 'Context.GetExternalFilesDir(string)'
I also already check this forum but found no luck
Any ideas how to solve this issue ?
To get privateExternalStorage you can use this
Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath;
I've followed this tutorial and running this code. File and folder creation examples work fine. But example to "Retrieve file contents", "Creating file under a folder" doesn't work. They keep giving error message like this: "DriveId is not found. Are you authorized to see this file". I've made sure that the "EXISTING_FILE_ID" does exist (first, created a file and then used its file id to retrieve contents). I'm not sure what am I doing wrong.
Anyone out there got these examples working ?
The demos you're pointing to do work in my environment, and the way you put the question is too broad to answer. More like a complaint. You have to be more specific if you want to get a specific help (code causing problem, ...). So there is nothing to answer here...
I can only point you to another piece of code, this DEMO, that may get you off-the-ground. It takes a different path to show the basic GDAA's CRUD functionality of Google Drive. You may have a better luck.
If I save or load data to or from the internal storage, it could cause an IOException. But I have no clue when this should happen. The only use case could probably be when you run out of space, but is there anything else which could cause this Exception here? (For example some right management configuration on the user side).
And when can this Exception occur while read operation? I don't even know any use case.
You wrote:
The only use case could probably be when you run out of space.
Other cases:
The parent directory does not exist. For example, you're trying to access /data/data/packagename/some_custom_dir_not_yet_created/my_file. You'd need to first call file.getParentFile().mkdirs().
The file you're trying to read does not exist. FileNotFoundException extends IOException.
You app is trying to read / write to another app's internal storage.
I know this is a pretty common question and I looked around the web and this forum for an answer but none of them seem to be working for me. I did the typical stuff like deleted my R.java and cleaned my project, made sure my class did not have a import for the R.java class. I tried rebuilding my project etc.
So here is what I have going on. I am trying to inflate a database from some raw SQL statements. I am using the book The Busy Coders Guide to Advanced Android Development book as a guide to do this.
it gives the following line of code:
InputStream stream=ctxt.getResources().openRawResource(R.raw.packaged_db);
and says the file is located within the res/raw directory like so "res/raw/packaged_db.txt"
I have placed my sql dump file: res/raw/raw_game_data.sql
and here is my line of code that is throwing the error:
InputStream inputStream = context.getResources().openRawResource(R.raw.raw_game_data);
any ideas or suggestions on what I am missing?
Thanks,
Generally, only one R.java is built for your entire project, and it is built in only one place, with one package - the package of your app, declared in your manifest. If that package is com.example.trek, and you refer to R in a class in com.example.trek.tribbles - or any other package - you will need to explicitly import com.example.trek.R
You list off some corrective measures without indicating that you understood the actual problem they were intended to correct, or if you had that problem. I'm not unsympathetic to this attitude in the context of Eclipse, but you can waste a lot of time this way. If you understand Java, then you only need to learn aapt to be able to drop down to the command line and attempt the process yourself.
That said, developers.android.com seem to have removed the documentation for aapt. ...well, don't worry, I'm sure you'll never have problems with Eclipse!
I have a text file within my raw folder of my app that I intend to use as a simple way to save settings and then read them back when needed. I can read from this file with using the BufferedReader and what comes with it, but I've tried a few different ways to be able to write to this file and none seem to work.
It seems to me that the problem is I never actually get the file, and I assume this is simply because I don't exactly know how I am supposed to give it the correct directory and file name. I've tried all I could come up with, and I tend to get errors like "No such file or directory exists" or "Read-only file system".
This seems to be a very simple problem relating to me just giving the wrong information, so if anybody could point me in the right direction it would be much appreciated.
Thank you,
Raphy
For saving settings you should use SharedPreferences rather than coming up with a custom solution.
SharedPreferences documentation
Data Storage
I'm not entirely sure you can do that with stuff in the "raw" folder.
One approach would be to use the SharedPreference storage in the API. See in the datastorage section of the docs. It's perfect from what you describe neededing. Another approach would be to put the file on the SDCard and read and write it from there.