I can't find out the correct folder to load my audio file.
I'm using an Android device which has no SD card but if I connect it via USB to my PC I will see the directory "Phone" with the subdirectory "Music".
In that directory I placed a mp3 file "Play.mp3" and then I tried the following variations to get a file reference:
File file = new File("/Phone/Music/Play.mp3");
File file = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), "Play.mp3");
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "Play.mp3");
But in each case file.exists() returns false.
Related
I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
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);
How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)
After certain research and lookup over the internet I found that I cannot place these files in the assets folders as those files will be private to my application, I want the native pdf application to be invoked on a button press which redirects the user to the particular document. What I am confused about is how do I bundle the files with my application so that when the user downloads or installs my application these files get stored in her SD-Card.
copy your assests files to the sdcard and when clicking on the button open the files in the sdcard as like this
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/filepath/"+fileName);//give the file path here
if(file.exists())
{
// do action here
}
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.