The scenario I am going for is as follows:
Android application (apk) that is "generic", i.e. I do not want to recompile with different resources
APK will be "pre-installed" on devices and so will not be a "market" app
The application needs a custom configuration file that customizes it with text, etc. that can be different for each installation. (config file needs to be in same location with same name)
On app start-up the config file gets read and configures app according to the data in the config file
In essence, this would be a way to provision the application so that it takes on a specific look and feel based on the config file without having to recompile. Be able to load custom image files, text data, etc.
The problem is that the config file needs to be easily updated and "copied" to a non-rooted device without a SD card. So I need access to the a non-app specific location that is easily accessible via a USB connection and that the APK has access to at run-time. It seems that SharedPreferences and Android file IO is limited to the private app directory under /data/data/pkg/... or external storage. Any ideas would be greatly appreciated. Thanks.
Just thought I would update with at least a partial answer. At least some of my issue has to do with testing in debug mode on my Razr Maxx. When I am connected via USB debugging then the call to create a new file fails as follows:
06-06 10:04:30.512: W/System.err(2583): java.io.IOException: open failed: EACCES (Permission denied)
06-06 10:04:30.512: W/System.err(2583): at java.io.File.createNewFile(File.java:940)
When I am running the app standalone on my device or in an emulator then it all works as expected. Not sure if this is related specifically to Razr Maxx or some other issue?
My code that is working is (from: Write a file in external storage in Android) :
private void writeToSDFile(){
// Find the root of the external storage.
// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal
File root = android.os.Environment.getExternalStorageDirectory();
mStatusTV.append("\nExternal file system root: "+root);
// See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
file.createNewFile();
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
mStatusTV.append("Write File 1: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
mStatusTV.append("Write File 2: " + e.getMessage());
}
mStatusTV.append("\n\nFile written to "+file);
}
Related
My device has no SD card, but I can access the files of "Internal storage" in Windows explorer, in which there is a "tmp.txt".
The txt file was pasted via Windows explorer, now I want to read this file. My codes are:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"tmp.txt");
StringBuilder text = new StringBuilder();
try {
InputStream in = new FileInputStream(file);
...
}
catch (IOException e) {
}
But it's not working, it step to catch exception after new FileInputStream(file);, the debug information logs the file location actually is "/storage/emulated/0/tmp.txt", so it couldn't find the file.
Then I tried put the file in the Download directory using:
File fileDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(fileDir, "tmp.txt");
error log:exception e:
It's not working either, basically they are the same problem. Can I access files in this directory?
Read this: developer.android.com/guide/topics/data/data-storage You don't have the permission to read the storage, the permissions you put are for location services.
I have created a file using
` try {
File myFile = new File("myfile");
if (myFile.exists())
myFile.delete();
outputStream = openFileOutput("myfile", Context.MODE_PRIVATE);
Log.d("MyServiceActivity", "file written");
outputStream.write(User.getInstance().getUserId().getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
`
How do i view the file created manually. Thanks in advance
Since the file is saved with Context.MODE_PRIVATE it is into internal memory, if you dont have root you will be not able to see this file with your phone
The best aproach to view your file is to open device monitor or Android monitor that is place in your Android Studio IDE
Note: run your app in debug mode to be able to see internal memory
directories and files !
Be sure to have your device pluged to your pc to run it
Happy coding
I cannot find the file I created in my android app in Android File Transfer under /Android/data. My goal is to copy/edit a config file to the app data folder while I develop the app, and then later link it to a Google Drive file. But, even when I write a file in the app with the following:
FileOutPutStream outputStream;
try {
outputStream = mAppContext.openFileOutput("my_config.json", Context.MODE_WORLD_WRITABLE);
outputStream.write("test".getBytes());
outputStream.close();
Log.d("WRITE", "FILE WRITTEN!");
catch (Exception e) {
e.printStackTrace();
}
I get the debug log message the the file was written, but I cannot find it in Android File Transfer (there isn't even a directory for my app). I have also tried cd-ing into it from adb shell, as well as rebooting the device. What is the best way to quickly transfer a file into and out of an app directory? I am running 4.4.2 Kit Kat.
I am writing to a path mnt/sdcard/foldername. I get no errors and the file is written, but when I browse the internal storage of my phone I can not find the folder. Where would the folder be located? I have a galaxy nexus?
Code is here:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/statReporter/");
directory.mkdirs();
Log.d("Tag", directory.toString());
//Path and name of XML file
File file = new File(directory, appendTimeStamp("phoneNum") + ".csv");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//Write to CSV File
osw.write(message);
osw.write(',');
//Flush and close OutPutStreamWriter and close FileOutputStream
osw.flush();
osw.close();
fOut.close();
Log.d("File Logger:", "File write successfully!");
Cant find it in Windows Computer\Galaxy Nexus\Internal Storage but all other folders appear.
I used an app called OI File Managaer and I can view the folder and file on phone but how do I view it through Windows OS?
If you really want to find files on your device, I'd recommend one of the Google Play apps you can find (I personally like ASTRO File Manager) or, from the PC you can use (for instance) Android Commander (which, incidentally, will let you use the same file path structure you'll be using from within your developing environment).
I believe that Android devices will not actually show you all paths and available files when you browse it, for instance, with Windows Explorer.
If you're using Eclipse, in the DDMS perspective you can browse your device's file structure. On your right you have the "File Explorer" and as far as I remember, it's a pretty complete tool.
Don't use /mnt/sdcard for accessing external storage. You can use Environment.getExternalStorageDirectory() to access path to external storage. This may vary from device to device.
We have a USB port in our android tablet(version 4.0.3).
Pendrive File Systems Format are
NTFS
FAT32
When Pendrive File Systems Format are FAT32 File has been created Successfully. But When File Systems Format are NTFS, I got the Error Message as open failed: EACCESS (Permission denied).
I Need to create a New File from in the USB Pendrive. I have tried my sample code is
Button createFile = (Button) findViewById(R.id.createFile);
createFile.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
File root = new File("/mnt/usbhost1");
Runtime.getRuntime().exec("chmod 777 " + root.getAbsolutePath());
File myFile = new File(root,"createNew.txt");
myFile.createNewFile();
Toast.makeText(getBaseContext(), "Done Creating File", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
Here /usbhost1 is a Android tablet USB Path. Where I am mistaken. How to create a New File from in the NTFS File Systems Format.
Thanks in advance.
Regards
Bala
What you need is a way to enable support for NTFS in the kernel on your device. This could be achieved dynamically by building the ntfs-driver as loadable module (.ko file). This would need to be done for the specific version of the kernel that is running on your device.
Next you need a way to automatically load the module each time the systems restarts. this is also "do-able" in Android. You might want to try this app which does precisely that. i.e. load one or more kernel module(s) located anywhere on the Android device.
After this whenever one inserts a external-device(usb-drive) that has ntfs partitions, the kernel will be able to properly recognise and mount it. Hence apps can then access it at its proper location like "/mnt/usbhost1" etc.