Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

save file to internal memory in android?

I am downloading a file from server with help of a URL provided through web service. I am successful for every version of devices but getting exception in OS 4.1 devices. I am using below code:

public static Boolean 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();
                        return true;
                } catch (Exception e) {
                        e.printStackTrace();
                        return false;
                }
        }

I am getting java.io.FileNotFoundException at line c.getInputStream(); please suggestion me to solve this problem.

I am planning to use the internal memory but as user cant access the internal memory.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this code: Notice that CONTEXT creating the file could be an Activity/ApplicationContext/etc

public boolean downloadFile(final String path) {
    try {
        URL url = new URL(path);

        URLConnection ucon = url.openConnection();
        ucon.setReadTimeout(5000);
        ucon.setConnectTimeout(10000);

        InputStream is = ucon.getInputStream();
        BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

        File file = new File(CONTEXT.getDir("filesdir", Context.MODE_PRIVATE) + "/yourfile.png");

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        FileOutputStream outStream = new FileOutputStream(file);
        byte[] buff = new byte[5 * 1024];

        int len;
        while ((len = inStream.read(buff)) != -1) {
            outStream.write(buff, 0, len);
        }

        outStream.flush();
        outStream.close();
        inStream.close();

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

    return true;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...