I'm writing an airplane simulator program and want the user to be able create model files seperately using XML. Then, in the app they can select which model (XML file) to load. I have the XML files in a subdirectory on my SD card and can read it with my BLU phone but not my Motorola Photon II. With the Motorola I get a file not found when initializing the input stream. I have the following code...
In the Manifest I have set read external storage permission...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Here's the code to open the XML file...
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/SimFiles/Vehicles/" + "myModel.xml");
InputStream in = new BufferedInputStream(new FileInputStream(file));
It's pretty straight forward. So, why on one and not the other? Is it a permissions thing- one more strict than the other? Thanks.
Try using this...
File file = new File(Environment.getExternalStorageDirectory() + "/SimFiles/Vehicles/" + "myModel.xml");
InputStream in = new BufferedInputStream(new FileInputStream(file));
Related
I am sorry I am extremely new to android and I am lost. I successfully found out how to save a file on an android system, but every time I try to search for the file on my Android phone (which is where I have a copy of this app located) I am unable to locate it. I know it is there because the app would not start up with out it. How do you write a file that can be both used by the App AND searched by the user in the File Explorer. I am using a Galaxy Note 3 Android version 5.0 Any help would be appreciated.
private void WriteFile(String FileName, String FileCont, boolean isAppend){
File file = new File(getApplicationContext().getFilesDir().getAbsolutePath() + "/" + FileName);
try {
FileOutputStream stream = new FileOutputStream(file, isAppend);
stream.write(FileCont.getBytes());
stream.close();
}
catch(Exception ex){
}
}
Did set the permissions to write on external space in your manifest.xml?
For reference see
http://developer.android.com/reference/android/Manifest.permission.html
You have to set the "WRITE_EXTERNAL_STORAGE" permission to write on external disk. Just add the following line to your android manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It will implicitly contain the permission to read external storage as well ;).
I have created an android app that needs to create a folder and write text files on my external SD card(extSdCard). I am using the Galaxy S4 device and have written the following codes for that. I already know the path of /mnt/.. file and have created a string for it. The android manifest.xml file uses the permission.i have checked the codes in "adb logcat" in Cmd prompt and it does not give any error but doesn't create any folder. The device has also been checked unconnected with the PC. Would appreciate if you help me. Here is the code.
String externalFilePath="/mnt/extSdCard/tmp";
Log.d(TAG, "externalFilePath is: "+externalFilePath);
File myfile = new File(externalFilePath, "Hello");
First of all make sure that you have this line inside your manifest,xml, somewhere outside application tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then you can write a File doing this :
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/tmp/");
// creates if doesn't exists
dir.mkdir();
// create a File
File file = new File(dir, "Example.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
//this is the text that will be inside of the Example.txt
String data = "Hello world";
os.write(data.getBytes());
os.close();
Hope it helps :)
try this code to generate files under your application package
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// Replace DIRECTORY_PICTURES with your needs
File file = new File(path, "Hello");
Also make sure you have added the permissions
I need to download some pdf files into data/data/com.**.* folder.
Those files are application specific and only application should read and display it that's the reason storing on data/data/com.**.* folder.
Please let me know how to download into that folder and open/read it in the application.
I know how to download it into SD card, but I do not have idea to downloading to application specific folder.
Please let me know some code examples to do this and also I need to know the capacity/size of the data/data/com.**.* folder.
As long as you want write your own applications Data folder, you can create a FileOutputStream like this FileOutputStream out = new FileOutputStream("/data/data/com.**.*/somefile"); than use that output stream to save file. Using the same way you can create a FileInputStream and read the file after.
You will get Permission Denied if you try to access another application's data folder.
I am not sure for capacity but you can calculate the size of the data folder using this
File dataFolder = new File("/data/data/com.**.*/");
long size = folderSize(dataFolder);
...
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
lengthlong += folderSize(file);
}
return length;
}
Hi here i am attaching the link of a tutorial explained.
http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
and there are many discussions going on internet that you should root your phone in order to access the data from data/data folder and I am also attaching some links about the discussion, I hope these are also some of the links that are related to your question
where do i find app data in android
How to access data/data folder in Android device?
and as well as some links that makes out the things without rooting your phone i mean
You can get access to /data/data/com*.* without rooting the device
http://denniskubes.com/2012/09/25/read-android-data-folder-without-rooting/
To Write file
FileOutputStream out = new FileOutputStream("/data/data/your_package_name/file_name.xyz");
To Read file
FileInputStream fIn = new FileInputStream(new File("/data/data/your_package_name/file_name.xyz"));
Now you have your input stream , you can convert it in your file according to the file type .
I am giving you example if your file is contain String data the we can do something like below ,
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String mDataRow = "";
String mBuffer = "";
while ((mDataRow = myReader.readLine()) != null) {
mBuffer += mDataRow + "\n";
}
Remember to add write file permission to AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I was trying to open a file for reading.
When using: Scanner input = new Scanner(filename); the file could not be found
but when I used:
InputStream in = openFileInput(filename);
Scanner input = new Scanner(in);
It worked. Why was the first line of code wrong?
Files are stored on the device in a specific, application-dependent location, which is what I suppose openFileInput adds at the beginning of the file name. The final result (location + file name) is constructed as follows:
/data/data/<application-package>/files/<file-name>
Note also that the documentation states that the openFileInput parameter cannot contain path separators.
To avoid hard-coding the location path, which could in principle even be different from device to device, you can obtain a File object pointing to the storage directory by calling getFilesDir, and use it to read whatever file you would like to. For example:
File filesDir = getFilesDir();
Scanner input = new Scanner(new File(filesDir, filename));
Note that constructing a Scanner by passing a String as a parameter would result in the scanner working on the content of the string, i.e. interpreting it as the actual content to scan instead of as the name of a file to open.
This drove me crazy couple of minutes ago. I forgot to add this line to manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I would expect a permission denied message. But just got a file not found...
In your case: openFileInput opens a file in your private app data directory (/data/data/your.package/filename). This never fails. But the scanner tries to open it on the root path. So when you want to read a file from SD card than you would use Environement.getExternalStorageDirectory().getAbsolutePath() + "/" + filename.
Scanner sc = new Scanner(new File(filename));
Im trying to save data to sdCard first i tried to saave it privately within app directory on externalStorage using getExternalFilesDir but gives me nullPointerException so i tried the other way given below it worked but when i want to store files into a custom directory that i want to named myself it give me error:
FileOutputStream os;
dirName = "/mydirectory/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + dirName);
dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
ErrorLog:
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/myfile2.png (No such file or directory)
Your directory "/mnt/sdcard/mvc/mvc" may not exist. What about changing your path to store the image in the Environment.getExternalStorageDirectory() path and then working from there?
Also, as Robert pointed out, make sure you have write permission to external storage in your manifest.
Edit - to create directories:
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/mvc/mvc").mkdirs();
Then you can save a file to root + "/mvc/mvc/foo.png".
Have you requested permission to write onto SD card? Add the following string to you app manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should check if you have added the required permission android.permission-group.STORAGE to your app. Without that permission you won't be able to access anything on the SD-Card.
BTW: On the Android system I know the SD-card is mounted on /sdcard not /mnt/sdcard
I found this book to be very helpful: "Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets". I noticed a part that allows saving images and stuff to the SD card.