views:

130

answers:

2

I am trying to download a zip file that is just less than 22 mb on start. I changed the default BufferedInputStream after these exceptions, but still get an out of memory error.

public void downloadFromUrl(String fileName) {

        //avoid unknown host exception
        try {

            InetAddress i = InetAddress

                    .getByName("http://xxx/Android/" + fileName);

        } catch (UnknownHostException e1) {

            e1.printStackTrace();

        }

        try {

            URL url = new URL("http://xxx/Android/" + fileName);
            File file = new File(fileName);

            long startTime = System.currentTimeMillis();
            Log.d("DownloadManager", "download begining");
            Log.d("DownloadManager", "download url:" + url);
            Log.d("DownloadManager", "downloaded file name:" + fileName);

            URLConnection ucon = url.openConnection();

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, 23 * 1024);

            ByteArrayBuffer baf = new ByteArrayBuffer(23 * 1024);
            int current = 0;
            while ((current = bis.read()) != -1) {

                baf.append((byte) current);

            }

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.d("DownloadManager",
                    "download ready in"
                            + ((System.currentTimeMillis() - startTime) / 1000)
                            + " sec");

        } catch (IOException e) {

            Log.d("DownloadManager", "Error: " + e);

        }

    }



10-28 10:18:16.885: DEBUG/ImageManager(1804): download begining
10-28 10:18:16.885: DEBUG/ImageManager(1804): download url:http://xxx/Android/Samples.zip
10-28 10:18:16.885: DEBUG/ImageManager(1804): downloaded file name:Samples.zip
10-28 10:18:17.204: INFO/global(1804): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
10-28 10:18:17.845: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 1803 objects / 185168 bytes in 96ms
10-28 10:18:18.415: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 229 objects / 105296 bytes in 78ms
10-28 10:18:18.425: INFO/dalvikvm-heap(1804): Grow heap (frag case) to 3.184MB for 376848-byte allocation
10-28 10:18:18.545: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 109ms
10-28 10:18:20.555: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 44 objects / 190080 bytes in 65ms
10-28 10:18:20.575: INFO/dalvikvm-heap(1804): Grow heap (frag case) to 3.721MB for 753680-byte allocation
10-28 10:18:20.675: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 95ms
10-28 10:18:22.725: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 3 objects / 376904 bytes in 66ms
10-28 10:18:23.145: INFO/dalvikvm-heap(1804): Grow heap (frag case) to 4.799MB for 1507344-byte allocation
10-28 10:18:23.245: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 95ms
10-28 10:18:25.415: WARN/ActivityManager(59): Launch timeout has expired, giving up wake lock!
10-28 10:18:26.205: WARN/ActivityManager(59): Activity idle timeout for HistoryRecord{43ef1378 com.xxx.xxxe/.TabViewMain}
10-28 10:18:27.995: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 3 objects / 753736 bytes in 65ms
10-28 10:18:28.655: INFO/dalvikvm-heap(1804): Grow heap (frag case) to 6.956MB for 3014672-byte allocation
10-28 10:18:28.765: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 102ms
10-28 10:18:33.275: DEBUG/dalvikvm(123): GC_EXPLICIT freed 833 objects / 48984 bytes in 2059ms
10-28 10:18:37.275: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 3 objects / 1507400 bytes in 69ms
10-28 10:18:38.115: INFO/dalvikvm-heap(1804): Grow heap (frag case) to 11.268MB for 6029328-byte allocation
10-28 10:18:38.275: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 151ms
10-28 10:18:53.885: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 3 objects / 3014728 bytes in 70ms
10-28 10:18:53.895: INFO/dalvikvm-heap(1804): Forcing collection of SoftReferences for 12058640-byte allocation
10-28 10:18:54.055: DEBUG/dalvikvm(1804): GC_FOR_MALLOC freed 0 objects / 0 bytes in 158ms
10-28 10:18:54.055: ERROR/dalvikvm-heap(1804): Out of memory on a 12058640-byte allocation.

EDIT: Ok, I was able to get this (http://stackoverflow.com/questions/2121833/permission-to-write-to-the-sd-card example working), but even with the correct permissions, I get a permission denied error:

`10-2`8 11:33:15.478: WARN/System.err(1781): java.io.FileNotFoundException: /mnt/sdcard/testDirectory/Samples.zip (Permission denied)
+1  A: 

Why do you use a ByteArrayBuffer ? This is the problem, you are trying to store 22mb in it.

fedj
A: 

It's no wonder, you're trying to create a 22MB bytearray into your phone's RAM; most Android phones does not have that much RAM. What you want to do is to stream the file into SD card or internal storage:

// untested
final int BUFFER_SIZE = 23 * 1024;
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE);
FileOutputStream fos = new FileOutputStream(file);
byte[] baf = new byte[BUFFER_SIZE];
int actual = 0;
while (actual != -1) {
    fos.write(baf, 0, actual)
    actual = bis.read(baf, 0, BUFFER_SIZE);
}

fos.close();
Lie Ryan
Hmmm...this gives me a java.io.file not found exception (read only file system).
@user375566: make sure your application have the appropriate permissions to write files http://developer.android.com/guide/topics/security/security.html and you should decide whether you want internal storage or external storage http://developer.android.com/guide/topics/data/data-storage.html
Lie Ryan
I'm using external, and I think I have all the necessary permissions.
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Edited for semi-working capability, but still having permissions errors.
@user375566: If you're working on an emulator, then maybe you're experiencing this issue? http://code.google.com/p/android/issues/detail?id=75
Lie Ryan
Problem solved: Emulator SD card size didn't get set high enough.