In my application I am capturing photos and storing into the SD card.
The location of my file is:
photoPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/"
+ "MyFolder" + "/"+"ImageName";
But when I give clear data using the "device's clear data option" the file is not getting deleted.When I used the Samsung devices, it is getting deleted. But I am facing this problem when I trying with Lenovo devices.
Any one could you please share your suggestions?
I think that option is meant to clear just the data associated with the app internally.
You have chosen to store extra data on the SD card, and I don't think the system is meant to delete it. You should be managing that yourself.
Which brings up the question of why Samsung is able to delete that file? I do not know why.
May be it depends upon the device.
If you want to delete the entire content in a folder, try this code.
void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
DeleteRecursive(child);
fileOrDirectory.delete();
}
Related
I have a big problem. I want to save some files that I download from internet with my application, into my SD card fallowing this path:
/storage/9016-4EF8/Android/data/com.my.application/files
But I don't want to hardcode this string inside my code. So how can I do it programmatically?
I have already ask permission to write on external storage.
Currently inside my code there is this piece of code below:
File sdcard = new File(context.getExternalFilesDir(null).getAbsolutePath() + "/Video scaricati/");
But with it, my application saves the files inside:
/storage/emulated/0/Android/data/com.my.application/files/Video scaricati
that it isn't in my SD card.
How can I do it?
Take the second item returned by
File dirs[] =getExternalFilesDirs();
Check if the array contains more then one element before use. Not everybody puts a micro SD card in.
Try changing to Environment.getExternalStorageDirectory() like this
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video scaricati/");
I'm goig to be mad with a strange issue. If i create a folder inside my code as
directory_path = Environment.getExternalStorageDirectory()
+ "/" + context.getResources().getString(R.string.app_name);
directory = new File(directory_path);
if (!directory.exists()) {
directory.mkdirs();
}
a new folder is created inside /sdcard/ . If i try to print on logcat directory_path variable, the path is different: /storage/emulated/0/
and if i go to that path, i found another folder with the same name of the one created on /sdcard/ . This is a problem for me because when i try to write some data into that folder, everithing goes in the one on /storage/emulated/0 , and the other one (that is the folder i want use) remain empty.
Why?
Have you tried reading back the data? /storage/emulated/0/ is the new path introduced in JB to support multiple users on tablet. But as long as you access external files using Environment.getExternalStorageDirectory() it doesn't really matter where they really reside.
Here's some additional info: https://android.stackexchange.com/questions/35541/why-did-sdcard-turn-into-sdcard-0-with-4-2
/storage/emulated/0/: to my knowledge, this refers to the "emulated
MMC" ("owner part"). Usually this is the internal one. The "0" stands
for the user here, "0" is the first user aka device-owner. If you
create additional users, this number will increment for each.
/storage/emulated/legacy/ as before, but pointing to the part of the
currently working user (for the owner, this would be a symlink to
/storage/emulated/0/). So this path should bring every user to his
"part".
/sdcard/: According to a comment by Shywim, this is a symlink to...
/mnt/sdcard (Android < 4.0)
/storage/sdcard0 (Android 4.0+)
For more detail you can visit stackexchange
I am trying to read an image file from /mnt/sdcard/image.jpg into my ImageView. Here is my code:
Bitmap bmp = BitmapFactory.decodeFile("/mnt/sdcard/image.jpg");
webImage.setImageBitmap(bmp);
I have write external storage permission.
My code says bmp is null, even though the image resides in the root directory (I go to I Drive and image.jpg is there.)
What am I doing incorrectly?
There could be two cases:
1) If you image is in the root folder of sdcard then it is possible to to access it through
Environment.getExternalStorageDirectory().toString + File.separator + "yourimage.jpg"
2) But i guess in your case it is in the /mnt/sdcard/external_sd which your memory card of the device in that case try this:
Environment.getExternalStorageDirectory().toString + File.separator + "external_sd" + File.separator + "yourimage.jpg"
Replace the above two paths with yours in BitmapFactory.decodeFile("Replace Here...")
I suspect the card with the image isn't mounted under /mnt/sdcard/.
Since you are using a Motorola device, chances are that you have two mass storage devices (see this list, if your device is in there, this is the case). In this situation you have to use the Motorola "External" Storage API to get the path to your second mass storage.
Also in general: You can't rely on hardcoding paths to the SD-Card like this. The mountpoint differs across devices. Or might be named differently, some devices might have a flash storage instead of a card, and so on. In short: What works on your phone breaks on others. You can read a reliable path to the primary external storage by calling Environment.getExternalStorageDirectory() instead.
Thanks, I managed to resolve it. Here is what I found:
I went to project > clean
I disconnected the phone from the computer. When it is connected, the SD is mounted and you can't view data.
The image name can't start with numbers. I am using SDK 7.
I viewed LogCat and I didn't get any errors, however, saving to other system areas (like /Android/Data/) caused a FileNotFoundException - permissions error.
File destination = new File(Environment.getExternalStorageDirectory(), "image" +
DateHelper.getTodaysDate() + "_" + DateHelper.getCurrentTime() + ".jpg");
private View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Add extra to save full-image somewhere
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
}
};
Please check you LogCat for more information.
My guess is you have a Samsung device. Samsung tends to have an external_sd folder, so you might have added it to the wrong sdcard because Samsung often has an internal and an external one.
Your code is perfectly okay. Just check whether you are writing proper sd card path or not. May be for that reason you are getting null bitmap.
And "write external storage permission" is for writing to your sd card not for reading from sd card. Make this concept clear.
I've developed an app that records audio and saves sample on sd card. i'd like to add a bit more functionality for example at the moment it save the sample automatically, whereas i'd like the user to be able to specify a filename and then that sample is saved under that name on the sd card. also when playing a sample i'd like the user to be able to choose a sample from a list generated from the available samples on the sd card. this is my problem, i'm not sure which android components to use to generate a list from the samples on the sd card. can anyone point me in the right direction?
thanks
Use a ListView.
With the ListView you will create a list of your SDcard´s media.
ListView Example
UPDATE ::
yes Turtleboy you will save your media files inside a directory
"/sdcard/mymediafiles/"
then extract the file´s name to be displayed in your ListView!
File sdCardRoot = Environment.getExternalStorageDirectory();
File myDirectory = new File(sdCardRoot, "/mymediafiles/");
for (File f : myDirectory.listFiles()) {
if (f.isFile())
Log.i("Jorgesys","*** Media File :: " + f.getName());
}
#Jorgesys suggestion of ListView is fine (+1), but you really need to think about what functionality you want to offer.
A listview would be fine if you just want to list file names.
If you have your files stroed in directories, then maybe a treeview, where you can expand and collapse the nodes (list windows explorer).
If you want to show multiple data about each sound sample (e.g file name, duration, artist, genre, etc) then use a stringgrid
I would like to make a file browser that will do two things:
1) Allow the user to browse and select a directory
2) Allow the user to browse all files on their sdcard
I've looked for tutorials but can't seem to find any?
Can someone please help me by either explaining how what my code would need to do in order to have a simple file browser or providing me with a link to a tutorial/source code?
Please and thanks!
If you're actually more interested in learning to write your own, I'd suggest taking a good long read through the File class documentation. That's where you're going to be doing most of the work.
In the case of SD cards/other external storage for Android, you'll want to first check to ensure that the external storage is mounted and available before trying to read it, using the Environment class:
String extState = Environment.getExternalStorageState();
//you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
//if you are only interested in reading the filesystem
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
//handle error here
}
else {
//do your file work here
}
Once you've determined the proper state of the external storage, a simple way to start is to use File's listFiles() method, like so:
//there is also getRootDirectory(), getDataDirectory(), etc. in the docs
File sd = Environment.getExternalStorageDirectory();
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();
You can then start using FileFilters to narrow down your results:
FileFilter filterDirectoriesOnly = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] sdDirectories = sd.listFiles(filterDirectoriesOnly);
From there on, just read through the docs to find the type of thing you're looking to do with it, and then you can work on tying these into list adapters, etc.
Hope this helps!
This is a late answer but I worked on creating an android file explorer recently. https://github.com/mburman/Android-File-Explore
Its really straightforward. Essentially its just 1 file that you would need to integrate into your application.
Take a look at OI File Manager, which is an open-source Android file manager. You can get the source code here.