I am new to android. I am trying to download a file from server and save the file in a particular path. The application is working fine. However I am not able to find the path of the downloaded file. In the code I have given as
File output = new File("/data/data/com.test.firstApp/", fileName);
Where can i find the file on my system?
Don't use hard coded file paths. The framework will give you the base path of the area you want to save files to.
For the SD card, use Environment.getExternalStorageDirectory()
For local files, use Context.getFilesDir() (or Context.openFileOutput(String name, int mode), etc)
For local cache, use Context.getCacheDir()
Adding to Rich's answer, in the likely event you will end up writing to external storage make sure to include this permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Check this page http://developer.android.com/guide/topics/data/data-storage.html
You can find there many methods how to save file. What is more you can also read something about good practices.
Cheers
Related
I have a file gpac.xlsx in the internal storage of my phone. I want to get its path. In android, it's confusing. Which one of these is the correct path?
/data/sdcard0/gpac.xlsx
/data/emulated/0/gpac.xlsx
Or some other besides these?
I have a file gpac.xlsx in the internal storage of my phone.
Based on your sample paths, your file is in what the Android SDK refers to as external storage.
I want to get its path.
new File(Environment.getExternalStorageDirectory(), "gpac.xlsx");
Which one of these is the correct path?
It could be either of those or something else. Use the Java snippet shown above to derive the proper path at runtime for the particular device that your code happens to be running on.
I've got c++ code in which I try to get a file from a directory on an android device. I've tried different ways to set the path which I pass to the fopen() function like:
/Android/data/com.myapp/files/Blip.wav
There actually is this file. But I guess that this is not a proper way to write a path. (The example was obtained by the java code )
getContext().getApplicationContext().getFilesDir().getPath() + "/Blip.wav"
There actually is this file
Since I have never seen an Android device with an /Android directory, that is unlikely.
What would fit is if you are looking at /Android/data/com.myapp/files/Blip.wav in a desktop file manager, using a USB or similar connection. In that case, Android/data/com.myapp/files/Blip.wav is a relative path in external storage. Specifically, it maps to:
new File(getContext().getExternalFilesDir(), "Blip.wav")
Try using this.
File root=Environment.getExternalStorageDirectory();
File file=new File(root,"/PersonData/Blip.wav");
Here personData is the name of folder
Currently I am able to download a file off the internet and store on the SD card, then use the file from there. However that makes the file (with proprietary data) available to be seen. I would prefer to use the file from somewhere like raw or assets folder.
I will skip the downloading code, but my code to use the file is this
File myFile = new File (android.os.Environment.getExternalStorageDirectory() + "/folder/filename.xml");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(myFile));
Android opens the file with the default application and all is good.
I have found similar Q/A's that revolve around using code like
Uri.parse("android.resource://com.projectname.testing/raw/filename");
and
InputStream ins = getResources().openRawResource(R.raw.filename);
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
I would like to solve my problem by simply accessing the file as a file. However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. This sounds like a lot of extra work, especially since my code above does work for a file stored on the SD card.
Does anyone have any ideas?
Thanks
I would prefer to use the file from somewhere like raw or assets folder.
Note that these too can be "seen".
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
setData() does not take a File. It takes a Uri. Use Uri.parse() to parse other types of Uri values -- you already have this code shown above.
However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes.
It definitely will need to be world-readable. Also, not all apps support all schemes, so apps that support file:// or http:// might not support android.resource://.
I want to create a folder in sdcard in an android device and I want to set a folder permission for the creating folder. The folder permission should be that from only from my application this folder should be assessable. No other application should be able to access this folder. Is there any method to create this. Please provide any valid link for this.
sadly it is not possible to create read-only permissions for the sdcard. This is a part of Android's system design and won't be changed soon.
I am making an application in android and I want to make a ready only folder in sdcard.
This is not possible, sorry.
FAT32, the standard SD card file system, does not support file permissions. This means that any file is world readable and world writeable.
String SaveFolder = "/Ready";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myFolder = new File(extStorageDirectory + Folder);
myFolder.mkdir();
This is a code to make a folder, and just like Tim said: it is not possible to create read-only permissions folder.
As others have said, you can't for the reasons specified.
I'm not exactly sure what you are trying to accomplish, but if you want to create a private file that can't be accessed by other apps, you can create in your app's "data" directory, by doing this,
OutputStream os = context.createFileOutput("myfile.ext", Context.MODE_PRIVATE);
Note that this is not the SD card, it's internal storage, so you aren't going to be able to create a very large file.
I need to place a file on a specific folder, so that my application can read it. First i want to try to create a new file with a specific path, but anyway i try, on the android dev tool on eclipse, I have an IOException.
Do you know a way to create "helloworld.txt" - For example - on the virtual device?
Thanks.
PS: I tried "new File(Environment.get..., "helloworld.txt").mkdirs();", and stuffs like that
If you want to create a file in the sdcard, you should check if the sdcard is ready (more info here), apart from the fact you need this permission in the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Try not use hard-code the path of the sdcard, use instead the API for that.
On the other hand, if you want to create a private file on the phone storage you can use this methods:
FileOutputStream fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
You can not access system folder by using android app.You need to transfer the file from your system to Android Emulator's Virtual SD Card by using file explorer availble with ADT plugin for android in eclipse.
Open file explorer in eclipse and import the file to device.