It's been two days now I'm stucked on a veeery common and simple problem I don't seem to be able to solve (while other people are):
creating a folder on a SD card on Android!!! YES!!!
I red many many posts here, many tutorials that seems to say the same thing:
create a string with the external folder path + /yourFolderName
create a new file passing the path as argument
call mkdir() (or mkdirs()) on it
done!
alongside you can check if the SD card is MOUNTED, READABLE, WRITABLE,
and of course don't forget to put in your Manifest.xml the permission to WRITE_EXTERNAL_STORAGE, but be careful, it must be set as direct child of the manifest and not of the application!!!
Well nothing of that seems to work for me.
The folder_creation code is inside onCreate() and I'm trying to call a MediaScannerConnection to test if the file exists but it return null on OnScanCompleteListener - but I'm not sure I'm using this in the correct way-.
The application runs fine (launch the default camera activity then returns to the main one), but the folder is not created! (by now I just want to create the folder with nothing inside)
Maybe is an issue related to the package name containing the word "example" included in my package name? (I red something related somewhere...)
What could be wrong? Please give me an hint, advice, something...
I'm building on a Mac 1.7.5 with Eclipse using minSdkVersion = 8 and testing on a HTC Wildfire S with 2.3.5 (sdkVersion = 10). Checking with ES File Manager 1.6.1.6 on a non rooted device
Here's the main part of the code driving me crazy...
I would be very glad to know there's something I can do...
Thank you in advance!
myPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
+ "myFolder";
File newDirectory = new File(myPath);
Log.v("judy says", newDirectory.toString());
newDirectory.mkdir(); // this doesn't work because "no directory" is displayed in the logCat window
if (!newDirectory.exists()) {
newDirectory.mkdir();
Log.v("no, no, no", "no directory");
}
MediaScannerConnection.scanFile(this,
new String[] { newDirectory.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
This works, if you want to create a directory "/mnt/sdcard/somedir/newdir"
File temp = new File (Environment.getExternalStorageDirectory (),
"somedir" + File.separator + "newdir");
if (!temp.exists ())
temp.mkdirs ();
ok, I solved: Simply using "uses permission" instead of "permission", the problem was that I was hard writing it in the manifest.xml, instead of using the graphical interface, this way I could use the built in list to choose a permission...
Really sorry if I bored...
I hope this could be useful for someone.
Related
To make visible to my folder from Windows with USB connection (MTP) : /storage/emulated/0/MyFolder
I put a dummy file on this folder and use MediaScannerConnection.scanFile to scan this file.
File file = new File(Environment.getExternalStorageDirectory() + "MyFolder" + File.separator + "dummy.txt");
MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, null);
All work well at the first time, i see that folder and dymmy file on windows. But if i delete the whole folder, when folder are re-created, it is seen as a file of 4K on windows.
Is there any cache on this level ? And how can i refresh this cache ?
Thank you
This bug of MTP still persists :
https://issuetracker.google.com/issues/36956498
https://issuetracker.google.com/issues/37071807
MediaScannerConnection.scanFile is not a perfect solution.
Please take a look at my code bellow. Basically, I was able to erase the information contained in my file, but the empty file still exists on my android device. Instead of completely erasing my file, it only removes the information contained in the file.
public void removeCache(Uri uri){
File delete= new File(uri.getPath());
if(delete.exists()){
if(delete.delete()){
Log.d("Deleted", ""+uri);
}
boolean exists= delete.exists();
Log.d("Deleted", "does it exist? " + exists);
}
}
Log Message:
D/Deleted: /storage/emulated/0/secretVideos/NHLPROMO.mp4
D/Deleted: does it exist? false
UPDATE:
So basically, the file does not exist in its direct directory, but download folder in the Android default file viewer stills shows deleted files. This is really annoying and should be removed by google. As of now I do not see any solution to this problem.
File file = new File(audio.getPath());
MediaScannerConnection.scanFile(context,
new String[]{file.toString()},
null, null);
You can try to update the path to see if it still exists or if there is any data
I am trying to create a folder and then after that do some file IO operations!
I am using a sony Xperia Z to test this out!
I know right now I've hardcoded the location but it doesn't let me create folders!
File appPath = new File("/storage/sdcard1/folder");
if (!appPath.exists()) {
appPath.mkdirs();
}
I am using a targetSdkVersion of 22
And having lollipop on my phone.
I tried
appPath.mkDir();
as well but all this gives a value of False.
And i have added permissions to manifest
<uses-permission name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission name="android.permission.READ_EXTERNAL_STORAGE" />
And I tried many different open source file manager but none are able to create folders, But ES file Manager is able to create folders and do File IO operations!
Don't hardcode global paths like /sdcard, use Environment.getExternalStorageDirectory() and related methods instead. Here is a working sample.
public static String getNewFolderPath() {
File folder = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "folder");
if (!folder.exists())
folder.mkdirs();
return folder.getAbsolutePath();
}
EDIT:
For more info check:
Find an external SD card location
#CommonsWare Answer
#Aleadam Answer
Hope it helps!
I've a problem with reading a XML file in my APP. I've tried multiple options but I'm getting error that read acces is false. I'm sure I'm using the correct path.
XML = new File(XML, "file.xml");
Log.i("XML", "Read access:" + XML.canRead());
This does return a false, I only need to read not to write (at least, not yet..).
System.getProperty(XML.getPath())
Returns null
I think there is a problem with the SD card (see other issue). But in my app I can open diffent folders except the XML file in the last folder.
I've added the correct permission to the manifest file.
Any help is very much appreciated. Thank you.
Edit:
File path:
File f = new File(Environment.getExternalStorageDirectory().toString()
+ File.separator
+ "external_sd"
+ File.separator
+ "app"
+ File.separator
+ "Games"
+ File.separator
+ "Version_1"
);
I've no problem with opening the folders. In my last folder there wil be a xml which can't be opened.
I'm using part of the path in other parts of the APP and there it works..
Also the code XML.exists() returns false...
Was related to to other issue, that one also solved this issue
all thanks for the help..
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.