I have a problem with understanding how to manage files at SDcard in the tablet.
I have my own application in Kotlin (in manifest.xml I have a permission READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE) and I need to have a folder "SongBook" in the root of SDCard. In this folder will be stored audio files (mp3) and HTML files. I read files without problems, but I need to create and delete these files. Now I use a File.
var file = File(Path_to_file)
try { file.delete() }
catch (e: Exception) { println("exception: "+ e.toString()) }
To create a file and write data to it, I use FileWriter. As I wrote, my app has permissions, but the file isn't deleted (or isn't created a new file).
I know about Android 4.4 KitKat's idea that applications not have access to SDcard to write. Apps may use a 'providers folder' with all permissions. But this folder is deleted after uninstalling application - no way for me. Data stored in "SongBook" is my own (like photos or videos) and must stay stored on SDcard after application uninstall.
Please, I need help, is any correct way to do this (without rooting android device) and using a folder in the root of SDcard to read/write/delete/create files?
Thx for responses (and sorry for my English).
I'm trying to read a file from a folder on my Galaxy S4. When I place the file in the root directory, I can access it without problems:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFile");
But I want to put my file in a subdirectory. When I make a folder 'A', place my file inside of it and try to access it with:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "A", "MyFile");
I can't read it. Do I need some kind of permission and what's the logic behind that, when I can access the root?
You can read here:
http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
that access to this folder is not recomended:
Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.
Also, permission is required:
Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.
[edit]
Also, since you are using s4 - which is probably 4.4+ device, you should know that since KitKat Google has disallowed write access on removable media, you can only read it, or write it to your application folder:
http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html
As a result, apps can read files on removable media using the various undocumented and unsupported tricks for finding removable media. However, apps cannot write to or otherwise modify such removable storage. Note that device manufacturers themselves may have ways of dealing with this, but ordinary app developers do not.
I am trying to read a file from the SD card, but finding that I don't have read permissions.
I first get the public storage directory and list the files like so:
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] files = dir.listFiles();
Then I select one of the files and test to see if it is readable:
Log.d(TAG, files[0].canRead());
This always displays false, and I'm not sure why. I am able to write to the directory (in fact it's the file that I've written that I want to read), and I've tried variations of
files[0].setReadable(true);
and
dir.setReadable(true);
with no luck. I also have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
included in my Manifest file and made sure my device wasn't connected to anything (as suggested here), but that wasn't it.
I am able to pull the file from the device and read it in a text editor. Any ideas as to what this could be?
Stats: Using API 12 on Samsung Galaxy tablet.
I want to display image from a sd card into image view
Following code works in emulator but does not work on actual phone
File f = new File(imgPath);
if (f.exists()) {
imgView.setImageDrawable(Drawable.createFromPath("/mnt/sdcard/abc.jpg") ));
} else {
imgView.setImageResource( R.drawable.image_abasent);
}
abc.jpg is put in emulator sd card sdk folder using DDMS
I have also put abc.jpg directly on SD card through USB connection
I have added following permission too in the manifest file (but while installing from apk permission is not asked though)
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Still on phone the desired image is not accessible.
What else is to be added or modified?
Please paste your manifest and also for accessing the SDCard you must use Environment.getExternalStorageDirectory() since /mnt/sdcard/ would be a hardcoded solution and may not work on some devices or android builds.
I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (StackOverflow) and tried the solutions suggested with no success. The simplest solution, it seems, would be the one suggested here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal but I cannot get this to work on my test device. I have also tried to create the file using the methods available in the java.io.File with the appropriate methods. I have also tried to create the file on the SDCard with the same result, fail. I have tried many solutions listed in other answers, following the code and instructions suggested exactly and find the same result. I am beginning to feel that I am missing some important bit of code, or a setting flag somewhere, I have set the permission in the manifest file:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
To be clear, I am trying to write to the device's internal, private storage. It is a small file containing a name, phone number, and a couple of type int flags. What ever method I use, I either find that the file did not create (or change if I place the file manually on the SDCard), or I get a NullPointerException when I try to reference the file or file location:
private File fILE = new File("Mydata", main.FILENAME);
or
private File fILE = getDir("Mydata", 0);
I am running the code on a HTC Hero, updated with the latest service release from Sprint. Any help would be GREATLY appreciated, Thanks in advance!
-Steve
Update (2/2/11): Using a EVO (API 8) I still get a NullPointerException. The code generating the exception is below, any thoughts on why my app can't access the internal storage? I have this problem on three different physical devices using two API levels (API 7 and 8).
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
UPDATE 2: 2/4/11 - I have found that I cannot see the file structure on the physical device (data directory) under any circumstance. Any one have any thoughts on this? The device is properly configured and can run app from eclipse or adb.
UPDATE 3: (2/9/11) - I think I may have found what the problem is here, but I am not sure about how to deal with it. I have figured out that the permissions on the /data/ directory on the physical devices are: drwxrwx--x. I am not sure why it is this way, maybe something to with Sprint? I have found this set this way on an HTC Hero, Samsung Epic (Galaxy S), and HTC EVO all on Sprint. The issue appears to be that DDMS and my app do not have r/w access to the directory. I need to figure out 2 things here, why it is like this and how to over come this issue in the wild. Again, any help here would be AWESOME!!
UPDATE 4: I think last February was a total blonde moment for me (see UPDATE 3). The test devices that I have are not ROOTed and hence no access (DUH!). After all the updates that he SGS and the EVO 4G have gone through, the result is still the same. I am still working this problem and will try and get back here with an update soon (hopefully less than a year next time).
Try changing the following line:
File newfile = new File(this.getFilesDir() + "/data/files/", "sample.txt");
to:
File newfile = new File(this.getFilesDir() + "/","sample.txt");
Not a direct answer to your question, but nevertheless: I noticed that you don't want to store tons of data in your file. Would it be a sufficient alternative to use the Shared Preferences instead?
And perhaps even more interesting: does the problem occur even when you write to the Shared Preferences file instead?
http://developer.android.com/guide/topics/data/data-storage.html#pref
A physical device's /data/ directory is only available to the root user. Since the only realistic way to get this access on a physical device is to root the device, DDMS file explorer cannot get into this branch of the directory tree.
As to why the app will not write there, likely the issue is in the fact that I have signed the app with debug keys (not a big *NIX expert, but what appears to be the case here from my personal research).
I was dealing with the same issue. Finally, I found that you really don't have to give all file paths in order to create a new file in internal storage. Just mention the file name and it will be created under your app's package folder at the device. I did exactly mentioned here
And it works perfectly. I would say avoid mentioning Full file path like : /data/... in order to create a file (you need write permissions to create a file in such a manner). Let Android framework do the job for creating a private file for your app.
The internal storage area is sort of private to the application so the user must have root access(rooted device) to create and load the files. If you have rooted device this is how to write a file to the internal storage:
// Create a file in the Internal Storage
String fileName = "MyFile";
String content = "hello world";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
This should instantly create a file called MyFile with the content hello world. The internal storage directory is usually found in a special location specified by our app’s package name. In my case it is /data/data/[package name] and the files created are stored in a directory called files in that directory.
As #bianca says, you're better not using a file path. But instead, use only the filename to create a File. Something like this:
public static void saveTextToFile(Context context, String filename, String content) {
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
And to get the file, you can use:
File file = new File(context.getFilesDir(), filename);
Read more: Saving Files.