Android internal storage reading file - java

I have a problem with Android internal storage. I have created folder in package root folder calling getDir() and with MODE_WORLD_WRITEABLE because I want camera app to write captured image in this folder. Anyway, I can see that captured image is inside that folder with DDMS.
Problem is that I cannot read that file.
I tried to read file with this code:
File file = context.getDir("images", Context.MODE_WORLD_READABLE);
File image = new File(file, "image.jpeg");
if (image.canRead())
Log.w("read", "can read");
else
Log.w("read", "can't read");
And in LogCat there is only second message (can't read).
I have also tried to create FileInputStream with file name but I receive FileNotFoundException.
Can somebody tell me what I'm doing wrong here?
Thanks in advance
EDIT:
Reading file is correct but only problem is that when cam app is saving image to specified location, permission set by cam app to image file are -rwxrwx---. After changing permissions with
adb (chmod 777 image.jpeg)
I was able to read image. Interesting thing is that cam app is writing images files to sdcard with ----rwxr-x.
Is there any way to change file permission in runtime?

Why not put it in the default photo directory?
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
( From: http://developer.android.com/reference/android/os/Environment.html )
See also a more complete example invoking the camera app: http://developer.android.com/training/camera/photobasics.html

Referencing to Android developers you should use openFileOutput() and openFileInput() to work on the internal storage... you are not allowed / not able to make dirs there. Android does it itself.. Those data will be cleared when your application will be deleted.

Related

Why is android unable of finding my file?

I want to access a file in included in the folder of the app and I want to be able to access it localy. I also don't want to use methods like android.getresources() and just use
File file = new File( filePath );
My file is saved in app/src/main/assets/ball.obj
The problem is when I do File file = new File("app/src/main/assets/ball.obj");, it alwas gives me a FileNotFoundException. I also tried moving the file in other places and absolute pathes, but it still doesn't work. Please help.
My file is saved in app/src/main/assets/ball.obj
That is a relative path to a file on your development machine. Assets are not files on the filesystem of the device. You cannot use File to access an asset.
To get an InputStream on your asset, call getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets().
For your call to open(), you need to pass in the relative path within assets/ to the asset that you wish to read in. So, if you are in a method of your Activity, getAssets().open("ball.obj") will give you an InputStream that you can use to read in the contents of that particular asset.

Get absolute path of a file [Android]

I have an app in which I write a file into the apps file folder(with a Fileoutputstream).
When i want to access it again I use a Bufferedreader with the location:
"data/data/com.mycomp.myapp.rstats/files/stats".
This works on pretty much all devices because the app gets installed there. But I imagine if somebody puts it on an external SD card, the path will be different. How can i get the absolute path of the file location?
You can use.. But do check for storage type exist before use it.
File extStore = Environment.getExternalStorageDirectory();

Double posts using Java to move Photos and Text Files to Dropbox for IFTTT recipes

I'm using Java move a file into a dropbox folder, which if a new file or photo that gets added, will trigger an IFTTT.com recipe that will post it to twitter. I am using Apache Commons IO for the movefile method on File Utils to create a File, and then move it.
But first, I have it find a path to a Content Path folder with a contentFileArray, which creates a File that gives us the image that I want to use. Then it takes the image, and creates a File destination that is in the local Dropbox folder. I set the file name and add the file extension.
Then I move it into the dropbox folder... which then triggers an IFTTT recipe that lets you post a Tweet when a file enters the Dropbox folder... or if a photo image enters the Dropbox folder. The problem is that a .png triggers as a file and as a photo, so I double post. It activates the File recipe and the Photo recipe.
IFTTT doesn't have any documentation for how Dropbox's Triggers work on the website, but maybe I'm missing something with FileUtils.moveFile(). Maybe it creates a file at that location, then has us change the file name. This could cause it to be read twice.
File src = new File(messageObject.getContentpath()+"\\"+contentFileArray[0]);
String ext = "."+FilenameUtils.getExtension(contentFileArray[0]);
File dest = new File(messageObject.getDropboxpath()+"\\"+messageObject.getMessage()+ext);
FileUtils.moveFile(src, dest);
successfulContentPosts++;
Does calling this line, cause this file to be made at this actual file path?
File dest = new File(messageObject.getDropboxpath()+"\\"+messageObject.getMessage()+ext);

Java - Audio URL

I have a audio file stored in a folder within the project and I'm trying to call the sound file within the folder.
I am able to call the file using the full URL
new java.net.URL("file:C:\NetBeansProjects\Puzzle\audio\applause2.wav"));
But is there a way to shorten it so that if I move the project I don't require changing the code?
Thanks
File file = new File("audio\applause2.wav");
Assuming you keep the audio folder at the same level relative to the jar

Where to place a xml file in android project?

For my project I am using an xml file to fetch information. I know this can be stored inside assets folders or inside resources/raw folder. My issue is I have to modify the xml dom and write back to the file again as a persistent storage.
I have read that I cant write back to a file in assets folder. But no idea regarding the file in resource folder?
Where can I keep the file for this purpose? Is storing the file in sdcard the only possible option?
Thanks
I would include the file in res/raw and then create a local copy in the file directory for your app. On app init, you can check to see if it exists in the external storage and then if it does not, load it anew from res/raw.
Without going too far into the nitty-gritty, something like this perhaps:
File filesDir = context.getFilesDir();
File xml = new File(filesDir, YOUR_FILE_NAME);
if(xml.exists() {
/* load data from filesDir */
} else {
/* load data from res/raw and save to filesDir */
}
Is storing the file in sdcard the only possible option?
Yes, and you can also store the file in internal memory.
You do not have the permission to modify the apk files.

Categories

Resources