2

I don't want to use bitmap's compress method. I just want to write directly into sd card. Don't want to use compression in my case.

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25
  • Most common image formats are compressed by nature (e.g., PNG, JPEG, WebP). What **specific** image format are you looking to use? – CommonsWare Feb 07 '17 at 14:58
  • I want image in PNG itself but don't want method to reduce its size or resolution. Save file as original. – Aman Srivastava Feb 07 '17 at 15:02
  • 1
    "don't want method to reduce its size or resolution" -- `compress()` never changes resolution. "Save file as original" -- since you did not provide a [mcve] demonstrating where the "original" is coming from, and since you have not defined what exactly you mean by "save file as original", it will be difficult for anyone to help you. – CommonsWare Feb 07 '17 at 15:07
  • I am getting image url from server which I open in browser it specification are as follows : 1000 * 665 and its size is 855kb. I use a imageloader to load this imageurl into imageview and save bitmap into sd card. But when I see image in android device its specification are as follows: 500 * 332, size = 142kb. I am writing bitmap using this method b.compress(Bitmap.CompressFormat.PNG, 100, writeStream); – Aman Srivastava Feb 07 '17 at 15:18
  • 1
    Most likely, your "imageloader" is altering your image, such as downsampling it to be only as large as is needed to show in your `ImageView`. However, since you still have not provided a [mcve], it is difficult to prove this point. Your problem does not lie with `compress()`, though, as `compress()` does not change the resolution from what the `Bitmap` is. – CommonsWare Feb 07 '17 at 15:22
  • Thanks...its imageloader which resizing image. – Aman Srivastava Feb 07 '17 at 15:55

1 Answers1

1

Based on the title: This saves the URL directly to a file:

    FileOutputStream output = ctx.openFileOutput(localFileName, context.MODE_PRIVATE);

            URLConnection openConnection = new URL(url).openConnection();
            openConnection.connect();
            InputStream inputStream = openConnection.getInputStream();
            byte[] buffer = new byte[1024];
            for (int n = inputStream.read(buffer); n >= 0; n = inputStream.read(buffer))
                output.write(buffer, 0, n);
            output.flush();
            output.close();
            inputStream.close();

You can change openFileOutput for a new FileOutputStream(new File("filepath")) to store on SDCard

But you look like are having problem with the ImageLoader API.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • does index 1024 sufficient for any image file size ? – Aman Srivastava Feb 07 '17 at 15:32
  • The buff is used for inputStream.read. Many discussions can be found around that number, but we usually use 1024 or 2048. http://stackoverflow.com/questions/8748960/how-do-you-decide-what-byte-size-to-use-for-inputstream-read – Marcos Vasconcelos Feb 07 '17 at 15:49
  • can you also tell me how to write a bitmap (already available with me) into file without use of compress method ? – Aman Srivastava Feb 07 '17 at 16:08
  • The only way to do it is to write the byte array directly into disk, but this makes the image unreadable because of the format, this solution involves into reading each byte and wirting into stream, and you need to decode the bitmap by yourself too. – Marcos Vasconcelos Feb 07 '17 at 16:13