SDCard content exist but cant see them
You probably need to index your files via MediaScannerConnection
. Quoting myself from a blog post from last year:
...the MTP contents are not based on the literal contents of external storage. Instead, MTP contents are based on what files have been scanned by MediaScannerConnection. If you write a file to external storage, until and unless that file is scanned by MediaScannerConnection, it will not be visible to users over MTP.
External storage is scanned on a reboot and possibly on a periodic basis. Users can manually force a scan via utilities like SDRescan. However, the best answer is for you to use scanFile() on MediaScannerConnection to update the media database after you close your file. This will make your file immediately available to the user.
Can't check if file on sdcard exists
I think that problem is here:
getBaseContext()
where it is assigned to NULL
. You really don't need this line. You can simply achieve your goal with
String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
File f = new File(path);
if (f.exists()) {
// do your stuff
}
else {
// do your stuff
}
Update:
If you or someone else have Samsung Galaxy S3, please follow @Raghunandan's answer because in this case getExternalStorageDirectory()
returns internal memory.
Created folder is not visible in the file explorer..
Damn! :)
Now I solved my problem...I was misunderstanding the operation of creating files in the file system.
When I spoke of file explorer I meant the file explorer of the operating system and NOT the file explorer in the DDMS :).
I thought when I create a file I will see it in the file explorer of the operating system but when the device is connected to the PC the files can only be seen in the DDMS file explorer.
Sorry I'm new to Android ;)
When the App is running standalone without PC connection and afterwards I connect with the PC I see the created files and folders of course :)
Thanks for help
creating a txt file on sd card but i cant find it
getExternalStorageDirectory() does not necessary return /sdcard directory. See here Environment.getExternalStorageDirectory does not return the path to the removable storage
SD card data sometimes NOT seen on PC when connected in USB mass storage mode (via phone)
Your problem is an exact duplicate of SDCard content exist but cant see them - the PC is displaying the content as returned from the MTP interface.
Stub code to do what you need to make the file appear:
imports:
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Log;
import java.io.File;
stub code:
File f = new File(Environment.getExternalStorageDirectory().getPath() + "/hello_nurse.txt");
if (! f.exists()) {
try {
f.createNewFile();
String[] files = new String[1];
files[0] = Environment.getExternalStorageDirectory().getPath() + "/hello_nurse.txt";
String[] mimes = new String[1];
mimes[0] = "text/plain";
MediaScannerConnection.scanFile(getApplicationContext(), files, mimes, null);
} catch (Exception ex) {
Log.e("SD Create", "Failed to create file", ex);
return;
}
} else {
Log.e("SD Create", "File is already present");
}
Unable to see file in Windows Explorer while it is visible in Android file browser
Ok, I figured out why it was happening.
Actually even if we press "Back" button, the program keeps running and unless I go to Settings > Applications > Manage Applications > "Force Stop" <application>
I can't access the file written by this program even if it is on sd card and even if the filewriter has been closed.
This is just based on my observation and I'd like someone to post a better answer with facts and with solution.
edit: I'll update my question accordingly.
edit: as recommended on meta I've posted a new question
Can't see a file in Windows written by an android app on sd-card unless I “Force Close” the app
Folder on phone not showing in Windows PC
Your pc will communicate with the media store about files. Not directly with the sdcard or external memory. If the media store does not know about your file your pc can not see it. You forgot to tell the store that you created a file. For every new file you should invoke the media scanner for that file. You are not the first one who happens this so the problem has been reported many times. You only need to add a few lines of code which you will find easily searching this site for invoking media scanner on new file. If you switch off/on your device the file will be known soon too.
Related Topics
Android - Taking Photos and Saving Them with a Custom Name to a Custom Destination via Intent
Cli on Dalvikvm Fails on Jni Lib
Android Access to Remote SQL Database
Writing/Reading Files To/From Android Phone's Internal Memory
Android Draw Route on a Mapview with Twoo Poi-S
Center Align Title in Action Bar Using Styles in Android
How to Read Text File in Android
Android: Get Facebook Friends List
Nexus 5X Reverse Landscape Sensor Fix in a Android Camera Preview App
Garbage Collection Causes:Mediaplayer Finalized Without Being Released
Android: Skimagedecoder:: Factory Returned Null
Listview Items Are Not Clickable. Why
How to Display Gif in React-Native Android App
Elevation on Android Lollipop Not Working
Read Contents of a Url in Android