Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a pdf file and save it to sdcard and then read it from there [duplicate]

Tags:

android

Possible Duplicate:
Download PDF from url and read it

I have to download a pdf file from an url and save it to the sd card and then to read it. I got through many codes and i found this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("*url for your pdf*"));
startActivity(browserIntent);

but how to save it in sd-card in my desired path and then read it from there.

like image 519
droid Avatar asked Mar 18 '12 14:03

droid


People also ask

Can you save PDF to SD card?

Move Files to External Storage SpaceIn the “Documents” tab, long tap on the target file until it can be selected. Tap the “More Actions” button in the top right corner. Choose “Move” and then switch to the “SD Card” page to browse all the folders stored on your device.

When I download a PDF where does it go?

They should be in the download folder. Go to the apps screen and you should see one called 'Downloads'.


1 Answers

Please take a look at this link.

It Contains an example of your requirement. Below there is a summary of the information in the link.

First step declaring persmissions in AndroidManifest.xml

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

Create a downloader class

public class Downloader {

    public static void DownloadFile(String fileURL, File directory) {
        try {

            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Finally creating an activity which downloads the PDF file from internet,

public class PDFFromServerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String extStorageDirectory = Environment.getExternalStorageDirectory()
        .toString();
        File folder = new File(extStorageDirectory, "pdf");
        folder.mkdir();
        File file = new File(folder, "Read.pdf");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Downloader.DownloadFile("http://www.nmu.ac.in/ejournals/aspx/courselist.pdf", file);

        showPdf();
    }
    public void showPdf()
        {
            File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
            PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
}
like image 197
Lucifer Avatar answered Oct 22 '22 06:10

Lucifer