Search code examples
androidandroid-networkingandroid-download-managerandroid-internet

download data in byte[] in android


I want download data in a byte[] and when save it in a jpg file it's show image. I can do it but I have problem. if I use this code to download data it doesn't have any problem but in this case the download speed is low :

String urlAddress = urlAddresses[0];
            try {
                URL url = new URL(urlAddress);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();

                int fileLength = httpURLConnection.getContentLength();
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] buffer = new byte[1];
                int curLength = 0;
                int newLength = 0;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                while((newLength = inputStream.read(buffer))>0)
                {
                    curLength += newLength;
                    publishProgress(fileLength != 0 ? (curLength*100)/fileLength : -1);
                    byteArrayOutputStream.write(buffer);
                }

                if(fileLength > 0 && curLength != fileLength)
                {
                    Byte[] result = new Byte[1];
                    result[0] = ERROR_FILE_LENGTH;
                    return result;
                }
                complate = true;
                byte[] resultByteArray = byteArrayOutputStream.toByteArray();
                Byte[] result = new Byte[resultByteArray.length];
                for(int i=0; i<result.length; i++)
                    result[i] = resultByteArray[i];
                return result;

            } 
            catch (IOException e)
            {
                e.printStackTrace();
                Byte[] result = new Byte[1];
                result[0] = ERROR_DOWNLOAD_FAILED;
                return result;
            }

but if i change this line : byte[] buffer = new byte[1] to byte[] buffer = new byte[1024] download speed go up but when I save it in a somename.jpg file it doesn't show image

String urlAddress = urlAddresses[0];
            try {
                URL url = new URL(urlAddress);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                httpURLConnection.connect();

                int fileLength = httpURLConnection.getContentLength();
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] buffer = new byte[1024];
                int curLength = 0;
                int newLength = 0;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                while((newLength = inputStream.read(buffer))>0)
                {
                    curLength += newLength;
                    publishProgress(fileLength != 0 ? (curLength*100)/fileLength : -1);
                    byteArrayOutputStream.write(buffer);
                }

                if(fileLength > 0 && curLength != fileLength)
                {
                    Byte[] result = new Byte[1];
                    result[0] = ERROR_FILE_LENGTH;
                    return result;
                }
                complate = true;
                byte[] resultByteArray = byteArrayOutputStream.toByteArray();
                Byte[] result = new Byte[resultByteArray.length];
                for(int i=0; i<result.length; i++)
                    result[i] = resultByteArray[i];
                return result;

            } 
            catch (IOException e)
            {
                e.printStackTrace();
                Byte[] result = new Byte[1];
                result[0] = ERROR_DOWNLOAD_FAILED;
                return result;
            }

note : in this case I want save data to just byte[] and save file normaly wit FileOutputStream and ... after I get full byte[] and download finished;

tahnks


Solution

  • change:

    byteArrayOutputStream.write(buffer); 
    

    to:

    byteArrayOutputStream.write(buffer, 0, newLength);