Jump to content



Photo

Some saved images cannot be viewed - Didn't download?

Answered Go to the full post android save images context

  • Please log in to reply
5 replies to this topic

#1 thatguyandrew1992

thatguyandrew1992

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 22-January 09

Posted 06 July 2013 - 07:37

Hey all,

I'm trying to save images onto an SD card but some of the images cannot load into 2 different gallery apps. When I transfer the images to my PC, I can see that all the ones that don't load on my phone, kind of didn't download. They have these green or gray gradients half way up the image. I guess they didn't finish downloading.

 

This code from my PC program works flawlessly, but when ported to android, I'm getting issues. I was reading about it online, and I thought maybe I need to use the openFileOutput and set the MODE_WORLD_READABLE. If this is a possible issue, can someone please explain how to use Context to get it to work?

 

I was saving images to a custom folder on my phone, but then I changed it to the Pictures folder. Same issue :(

ReadableByteChannel rbc2 = Channels.newChannel(link2.openStream());//Gets the image
FileOutputStream fos2 = new FileOutputStream(Methods.getMainDir() + File.separator + galleryName + File.separator + outputFileName);//The output of the file name
fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
fos2.flush();
fos2.close();


Best Answer thatguyandrew1992 , 07 July 2013 - 18:44

Well, it was something to do with that code I was using for downloading. I changed my code to the following and the issue seems to be gone! I'll keep testing, and if it works, I mark this as a solution

 

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

FileOutputStream fos = new FileOutputStream();
fos.write(response);
fos.close();

Go to the full post



#2 OP thatguyandrew1992

thatguyandrew1992

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 22-January 09

Posted 07 July 2013 - 03:21

Anyone?



#3 OP thatguyandrew1992

thatguyandrew1992

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 22-January 09

Posted 07 July 2013 - 08:00

Dont let me down Neowin! :)



#4 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 07 July 2013 - 10:55

How big are the images you're downloading. My first thought is that the use of "1 << 24" as the count param for fos2.getChannel().transferFrom(rbc2, 0, 1 << 24); could be an issue. The maths on that works out that you will only transfer a maximum of ~16MiB, which might be smaller than the size of the image. Personally I doubt it, but I don't know what kind of images you're downloading.

Aside from that, is an exception being thrown during the transfer?

#5 OP thatguyandrew1992

thatguyandrew1992

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 22-January 09

Posted 07 July 2013 - 18:21

How big are the images you're downloading. My first thought is that the use of "1 << 24" as the count param for fos2.getChannel().transferFrom(rbc2, 0, 1 << 24); could be an issue. The maths on that works out that you will only transfer a maximum of ~16MiB, which might be smaller than the size of the image. Personally I doubt it, but I don't know what kind of images you're downloading.

Aside from that, is an exception being thrown during the transfer?

No exceptions being thrown and the files aren't over 16mb. :/

 

What's really confusing me is that I have no issues on my desktop pc, just on this android phone...



#6 OP thatguyandrew1992

thatguyandrew1992

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 22-January 09

Posted 07 July 2013 - 18:44   Best Answer

Well, it was something to do with that code I was using for downloading. I changed my code to the following and the issue seems to be gone! I'll keep testing, and if it works, I mark this as a solution

 

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

FileOutputStream fos = new FileOutputStream();
fos.write(response);
fos.close();