File creation in SD card not working - android

I am trying to create a file and store it in SD Card to be used as an input for some processing for an apps.
After searching for a while, I got this code which can create a file in SD card.But after running this,I couldn't see any file created in my SD card. Can anyone please help me what I am missing here.
BufferedWriter out = new BufferedWriter(new FileWriter(FileDescriptor.err));
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
File perffile = new File(root, "samplefile.txt");
FileWriter perfwriter = new FileWriter(perffile, true);
out = new BufferedWriter(perfwriter);
}
} catch (IOException e) {
Log.e(TAG, "-Could not write file " + e.getMessage());
return;
}

If you want to add a file or folder or move application into your SD Card just do the following:
steps:
1) Open your Android application's source code file with a text or programming editor.
2) Browse to the location in the source code where you wish to call the function that writes a file to the device's external storage.
3) Insert this single line of code to check for the SD card:
File sdCard = Environment.getExternalStorageDirectory();
4) Insert these lines of code to set the directory and file name:
File dir = new File (sdcard.getAbsolutePath() + "/folder1/folder2");
dir.mkdirs();
File file = new File(dir, "example_file");
// The mkdirs funtion will create the directory folder for you, use it only you want to create a new one.
5) Replace "/folder1/folder2" in the above code with the actual path where you intend to save the file. This should be a location in which you normally save your application files. Also, change the "example_file" value to the actual file name you wish to use.
6) Insert the following line of code to output the file to the SD card:
FileOutputStream f = new FileOutputStream(file);
Finally step 7:
Save the file, then compile it and test the application using the Android emulator software or the device.
This will works!!! ;-)

Related

Nothing Happening when trying to write to file on SD card android

I am using the following code to write to an SD card:
File dir =new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
if(!dir.exists())
{
dir.mkdirs();
}
String filename= "MyDoople.txt";
try
{
File f = new File(dir+File.separator+filename);
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(
fOut);
myOutWriter.append("Mytest");
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Text Updated",
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
However when I run my app, and then go check in the SD card, there is nothing there. Why am I not seeing the file that I created? I am using android jellybean 4.1 and have added the write permissions in the manifest file.
From your code, you're writing to the folder "MyFolder" under primary external storage.
What is the device you are using? Does it have interal storage in additional to sd card? If yes, then your file is written to the internal storage, but not the sd card.
Edit:
To access SD Card, you simply replace android.os.Environment.getExternalStorageDirectory() with the sd card path.
It is not an easy task to find the path of SD card.
One method is to use ContextCompat.getExternalFilesDirs(context, null), the first element of the returned value would be the same android.os.Environment.getExternalStorageDirectory(), the second element would be somewhere of the sdcard.
However, could be depending on your android version, the directory returned could be a sub-directory on the sd card, i.e. your application specific directory instead of the root of SD card. You have to check and manually change it if you want to find the root directory.
http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#getExternalFilesDirs%28android.content.Context,%20java.lang.String%29

I can't write to the external SD card(extSdCard) in Android Studio

I have created an android app that needs to create a folder and write text files on my external SD card(extSdCard). I am using the Galaxy S4 device and have written the following codes for that. I already know the path of /mnt/.. file and have created a string for it. The android manifest.xml file uses the permission.i have checked the codes in "adb logcat" in Cmd prompt and it does not give any error but doesn't create any folder. The device has also been checked unconnected with the PC. Would appreciate if you help me. Here is the code.
String externalFilePath="/mnt/extSdCard/tmp";
Log.d(TAG, "externalFilePath is: "+externalFilePath);
File myfile = new File(externalFilePath, "Hello");
First of all make sure that you have this line inside your manifest,xml, somewhere outside application tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then you can write a File doing this :
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + "/tmp/");
// creates if doesn't exists
dir.mkdir();
// create a File
File file = new File(dir, "Example.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
//this is the text that will be inside of the Example.txt
String data = "Hello world";
os.write(data.getBytes());
os.close();
Hope it helps :)
try this code to generate files under your application package
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// Replace DIRECTORY_PICTURES with your needs
File file = new File(path, "Hello");
Also make sure you have added the permissions

How to save file on external SDCARD on android MotorolaARTIX2 device?

I would like to save a file on external SdCard.I have implemented an application for save a file on external sdcard.But my Android MotorolaARTIX2 device contains internal sdcard.When i am trying to save file on external sdcard it always saving to internal sdcard in my device.
I have implemented my application as follows:
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File file = new File(root, "myfile.txt");
FileWriter gpxwriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
Log.e("Exception", "Could not write file " + e.getMessage());
}
From the above code my application always saving myfile.txt file on internal sdcard but not external sdcard-ext.And my application is support all devices with same code.
How can i save myfile.txt on sdcard-ext(external) not on sdcard(internal) in my device?
please any body help me....
Motorola has an API for this. Look here: http://developer.motorola.com/docs/motorola-external-storage-api/ But that's not a good generic solution. You probably need to scan the filesystem for a more generic solution that will work on all devices.
Take a look at the answer from this question, especially the one from Baron

Problem to create directory in Android

I'm developing a simple application for Android devices.
I need to create a directory, but I can't do it.
File folder = new File(Environment.getExternalStorageDirectory ()+"/dir");
if(folder.mkdirs()){
CrearToast("Directorio creado"); //successfully created
}else{
CrearToast("fallo"); // error creating directory
}
*CrearToast creates a toast with the text in brackets.
I have set
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
are you trying to write on SD card or phone memory??
Have you looked at this??
http://developer.android.com/guide/topics/data/data-storage.html
EDIT:
This is how i create my folders
File CheckDirectory;
CheckDirectory = new File(FolderPath);
if (!CheckDirectory.exists()){
CheckDirectory.mkdir();
}
SD card directory:
Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

save image to sdcard android Directory problem

Im trying to save data to sdCard first i tried to saave it privately within app directory on externalStorage using getExternalFilesDir but gives me nullPointerException so i tried the other way given below it worked but when i want to store files into a custom directory that i want to named myself it give me error:
FileOutputStream os;
dirName = "/mydirectory/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + dirName);
dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
ErrorLog:
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/myfile2.png (No such file or directory)
Your directory "/mnt/sdcard/mvc/mvc" may not exist. What about changing your path to store the image in the Environment.getExternalStorageDirectory() path and then working from there?
Also, as Robert pointed out, make sure you have write permission to external storage in your manifest.
Edit - to create directories:
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/mvc/mvc").mkdirs();
Then you can save a file to root + "/mvc/mvc/foo.png".
Have you requested permission to write onto SD card? Add the following string to you app manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should check if you have added the required permission android.permission-group.STORAGE to your app. Without that permission you won't be able to access anything on the SD-Card.
BTW: On the Android system I know the SD-card is mounted on /sdcard not /mnt/sdcard
I found this book to be very helpful: "Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets". I noticed a part that allows saving images and stuff to the SD card.

Categories

Resources