CodingDecoding
CodingDecoding

Reputation: 443

Writing File in External SD card in android

I have tried so many ways to write file in external as card but not working. Please suggest me what to do.

The code snippet that I wrote is as follows:

final String directoryFile = "file:///"+"mnt/extsd/Test";
    String filename = "TextData.txt";
    Context context;

    //String file=Environment.getExternalStorageDirectory()+ "/TextData.txt";
    //String file = "mnt/extsd/TextData.txt";



    //String file=Environment.getExternalStorageDirectory()+ "/RudimentContent/test.txt";
    //File root = android.os.Environment.getExternalStorageDirectory(); 
    //File dir = new File (root.getAbsolutePath() + "/download");
    //File path = Environment.getExternalStorageDirectory();
    //String filename = "TextData.txt";
    //String fileName = "TextData.txt";
    //String path = "Environment.getExternalStorageDirectory()/TextData.txt";
    //File path = Environment.getExternalStorageDirectory();

public void onClick(View v) 
            {
                // write on SD card file data in the text box
                // dir.mkdirs();
                //File file = new File(dir, "myData.txt");



                //String fileName = surveyName + ".csv";
                //String headings = "Hello, world!";



                //File file = new File(path, fileName);
                //path.mkdirs();
                //OutputStream os = new FileOutputStream(file);
                //os.write(headings.getBytes());




                //create path

                //create file
                //File outFile = new File(Environment.getExternalStorageDirectory(), filename);


                //File directoryFile = new File("mnt/extsd", "Test");

                //directoryFile.mkdirs();

                //create file
                //File file = new File(Environment.getExternalStorageDirectory(), filename);

                try{
                    File myFile = new File(directoryFile, filename);                //device.txt
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);

                    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                    myOutWriter.append(txtData.getText());
                    myOutWriter.close();
                    fOut.close();
                    Toast.makeText(getBaseContext(),"Done writing SD "+myFile.getPath(),Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        });

I have commented on so may tried codes also. When I write in internal sd card then its working but not with external. Please suggest.

Upvotes: 0

Views: 9631

Answers (4)

Budius
Budius

Reputation: 39836

I had this before. The reason you're having this exception is due to some bizarre ways the framework handles files and folders.

on my case was that I was testing, and all was working, and I deleted the testing folder and since then the system keeps trying to write on the deleted folder. I removed the project from the phone and reboot it and started working again.

furthermore, I suggest you a quick reading on this answer What is the best way to create temporary files on Android? and the comments of this answer... as there is a lot of useful information if you want to create a good app.

Upvotes: 1

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

try this

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

and add this in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

EDIT: By using this line you can able to see stores images in the gallery view.

 sendBroadcast(new Intent(
 Intent.ACTION_MEDIA_MOUNTED,
          Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Upvotes: 0

Sebastian Breit
Sebastian Breit

Reputation: 6159

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary.

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data//files/

You will have to set the permissions too:

android.permission.WRITE_EXTERNAL_STORAGE

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 1611

just set permission like this

android.permission.WRITE_EXTERNAL_STORAGE

Upvotes: 0

Related Questions