I want to save some data in the user's external directory (ie. SD card), but there seems to be a weird problem. I'm using Environment.getExternalStorageDirectory() which returns "mnt/sdcard/" (which is fine). I want to create two folders on in this directory so I do:
File main = new File(getExternalStorageDirectory() + "/my_app/some_data");
if(!main.isDirectory())
main.mkdirs();
Now I thought this would make the directory "mnt/sdcard/my_app/some_data", but after using a file manager to look at the SD card, it turns out that this folder is created at "mnt/sdcard/my_app/mnt/sdcard/my_app/some_data", which is quite bizarre. Can anyone tell me how to fix this?
Try the following and see what you get...
String packageName = this.getPackageName();
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "files");
myFilesDir.mkdirs();
It's exacly what I use to create a working directory on an SD card. For me it creates...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
...where 'com.mycompany.myApp' is the actual package name of my app.
Related
Here's what I have so far:
var file = Android.OS.Environment.DirectoryPictures + "/test1.jpg";
ImagePhoto.Source = ImageSource.FromFile(file);
However, no image is being set. I've double checked that the test1.jpg file exists in the device's local storage Pictures folder, and it's there when I navigate to it using File Commander. Can anyone help?
I managed to solve this using the Environment variables:
var file = Android.OS.Environment.ExternalStorageDirectory + "/" + Android.OS.Environment.DirectoryPictures + "/test1.jpg";
My app downloads image files for offline use. Using the DownloadManager, the files are saved to the app's /data/ folder (retrieved with Environment.getDataDirectory().getAbsolutePath()). When displaying the images, the function File.exists for the path /data/filename returns false. When I provide the full path (kinda harcoded with the following function) everything works.
private static String getImageUri(Context c, String name) {
return Environment.getExternalStorageDirectory().getPath() + File.separator +
"Android" + File.separator + "data" + File.separator +
c.getPackageName() + File.separator +
"files" + File.separator + name;
}
It seems the File api requires the full path but I couldn't find a more elegant way of getting the full path.
Is there a better way of doing this? Another api that allows r/w and treats paths the same way the DownloadManager does? Or a better way of retrieving the full path?
The code returns the path storage/emulated/0/android/data/com.package.app/files/data.
I assume that your lowercase android is a typo, as that location should be Android.
You can obtain that location more simply via getExternalFilesDir() (method on Context), though you will need to append the trailing data segment yourself.
The line request.setDestinationInExternalFilesDir(context, Environment.getDataDirectory().getAbsolutePath(), name); works
It would be simpler — and far more compliant with the documentation — to just use request.setDestnationInExternalFilesDir(context, null, name). Then, your downloaded file would be in the location new File(getExternalFilesDir(null), name).
I'm trying to create an empty directory, but instead it creates a file.
publicDocsPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
autoTestDir = new File(publicDocsPath + File.separator+"autoTest");
autoTestDir.mkdirs();
I can get around it by creating a dummy file under neath it, as such
autoTestDir = new File(publicDocsPath + File.separator+"autoTest" + File.separator+ "nullfile");
But I would like to know if I'm doing something wrong or if there is a way to tell the system you want to create a directory not a file.
Note: I'm on a mac, and I'm using Android File Transfer program to verify my results. Maybe the file is being created as a directory, but issue is with Android File Transfer and it shows the directory as a file.
Try this
File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File dir = new File(documents.getAbsolutePath() + File.separator + "autoTest" + File.separator);
// creates if doesn't exists
dir.mkdir();
You have to add the file separator after "autoTest".
I've a file in my Download folder called random.txt and I want to display this in my android app. So I thought I could write this code to get the file path and open it:
String path= Environment.getExternalStorageDirectory()
.toString() + File.separator;
openPdfIntent(path + "/Download/random.pdf");
But I get the log message that my file doesn't exist. If I browse to the location with my file manager the file is there.
My phone is a HTC one so I don't have a external sdcard.
Remove File.separator from 'path' variable.
I forgot to give permission in my manifest.
That in combination with
String pdfFilePath = Environment.getExternalStorageDirectory()
.toString() + "/Download/random.pdf";
try this:
pdfFilePath = "file://" + Environment.getExternalStorageDirectory()
.toString() + "/Download/random.pdf";
I have read this tutorial about downloading database to my sd card and I have a question what will happen while the mobile phone does not have SD card?
Then I have to download data to internal memory.
So how to check if device has got sd card or not and then set appropriate location before downloading? or maybe it will be done automatically because it use:
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
Please help me if you know,
Thank you
Some of Android phones doesnt have an sdcard slot but the internal memory is treated and simulated as external sdcard. Check this before.
To check the sd-card state: getExternalStorageState()
If it has sdcard (or simulated sdcard)
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
If its better if you create a folder in the sdcard:
String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(sdpath + "/AppFolder");
dir.mkdir();
outFile = new File(dir.toString() + "/" + fileName);
IF the phone dosnt have a simulated sdcard:
outFile = new File(fileName);
The location is data/data/mypackage/
I think it can be useful Android Storage Options.
For database you can check sd-card state with getExternalStorageState(), or use Android SQL helper-class.
public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}