user819511
user819511

Reputation: 21

How to write files to an SD Card from the internet?

An example would be a simple image.

I have tried so many things and it just refuses to work despite making a whole lot of sense.

What I've done so far is I'm able to grab 25 pictures and add them to

/sdcard/app name/sub/dir/filename.jpg

They all appear there according to the DDMS but they always have a filesize of 0.

I'm guessing it's probably because of my input stream?

Here's my function that handles the downloading and saving.

public void DownloadPages()
{   
    for (int fileC = 0; fileC < pageAmount; fileC++)
    {

        URL url;
        String path = "/sdcard/Appname/sub/dir/";

        File file = new File(path, fileC + ".jpg");

        int size=0;
        byte[] buffer=null;

        try{
            url = new URL("http://images.bluegartr.com/bucket/gallery/56ca6f9f2ef43ab7349c0e6511edb6d6.png");
            InputStream in = url.openStream();

            size = in.available();  
            buffer = new byte[size];  
            in.read(buffer);  
            in.close();  
        }catch(Exception e){

        }

            if (!new File(path).exists())
                new File(path).mkdirs();

       FileOutputStream out;

       try{
           out = new FileOutputStream(file);
           out.write(buffer);  
           out.flush();  
           out.close();
       }catch(Exception e){

       }


    }

}

It just keeps giving me 25 files in that directory but all of their file sizes are zero. I have no idea why. This is practically the same code I've used in a java program.

PS...

If you're gonna give me a solution... I've already tried code like this. It doesn't work.

    try{
        url = new URL(urlString);
        in = new BufferedInputStream(url.openStream());
        fout = new FileOutputStream(filename);

        byte data[] = new byte[1024];
        int count;
        System.out.println("Now downloading File: " + filename.substring(0, filename.lastIndexOf(".")));
        while ((count = in.read(data, 0, 1024)) != -1){
            fout.write(data, 0, count);
        }
    }finally{
            System.out.println("Download complete.");
            if (in != null)
                    in.close();
            if (fout != null)
                    fout.close();
    }
}

Here's an image of what my directories look like

http://oi48.tinypic.com/2cpcprm.jpg

Upvotes: 0

Views: 289

Answers (2)

Kurru
Kurru

Reputation: 14321

Using Guava something like this should work:

String fileUrl = "xxx";
File file = null;

InputStream in;
FileOutputStream out;
try {
  Uri url = new URI(fileUrl);
  in = url.openStream();
  out = new FileOutputStream(file)
  ByteStreams.copy(in, out);
} 
catch (IOException e) {
  System.out.println(e.toString());
}
finally {
  in.close();
  out.flush();
  out.close();
}

Upvotes: 0

Lucifer
Lucifer

Reputation: 29632

A bit change to your second option, try it as following way,

byte data[] = new byte[1024];
long total = 0;

int count;

while ( ( count = input.read(data)) != -1 )
{
    total += count;
    output.write( data,0,count );
}

This one is different in while statement while ((count = in.read(data, 0, 1024)) != -1)

Upvotes: 1

Related Questions