Rivu Chakraborty
Rivu Chakraborty

Reputation: 1372

how to Save Public File in Android external storage

I am developing an android app in which I am downloading a pdf file from internet to the external directory. I have tried in many ways but there is always an error. I am listing my codes with errors serially below:

1.

String fname=title+".pdf";
loc="/sdcard"+"/"+fname;
output=new FileOutputStream(loc);

Error:

BookActivity(1255): /sdcard/Pragmatic Unit Testing in Java with JUnit.pdf: open failed: EACCES (Permission denied)

2.

File outputdir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String fname=title+".pdf";
loc=outputdir.getAbsolutePath()
output=new FileOutputStream(loc);

Error: BookActivity(1172): /storage/sdcard/Download/Node.js for PHP Developers.pdf: open failed: ENOENT (No such file or directory)

I have this permission in my AndroidManifest.xml file:

<!-- Permission: Allow Connect to Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission: Writing to SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />

Please help me out what to do?? Also if you can please tell me how to save public files in internal storage.

also tried the answer at https://stackoverflow.com/a/7887114/964741

but again arror:

BookActivity(1319): /storage/sdcard/Apache Axis2 Web Services, 2nd Edition.pdf: open failed: EACCES (Permission denied)

Upvotes: 0

Views: 3440

Answers (2)

Ganesh AB
Ganesh AB

Reputation: 4698

1> To write data file to sdcard :-

private void writeTextFileToSdcard(File fileObj, String fileName, String fileData) {

        fileObj.mkdirs();
        File file = new File(fileObj, fileName);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(out);
            myOutWriter.append(fileData);
            myOutWriter.close();
            out.flush();
            out.close();

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

2> To write Image to sdacrd :-

private void writeImageFileToSdcard(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/My_Images");    
    myDir.mkdirs();
    String fname = "Image1.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();
    }
}

Permission is mandatory

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

Upvotes: 1

No More Hello World
No More Hello World

Reputation: 435

in the first error you're using a wrong path (I guess). Since the 4.4 version (I'm not sure) they changed the way to access to external devices.

You should write:

String fname=title+".pdf";
loc="/mnt/media_rw/sdcard"+"/"+fname;
output=new FileOutputStream(loc);

I think it is the right way.

I defined the permissions in my manifest file like this:

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

I hope it helps.

Upvotes: 1

Related Questions