• 0

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


Question

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();
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

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 = ;
while (-1!=(n=in.read(buf)))
{
out.write(buf, , n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

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

Link to comment
Share on other sites

  • 0

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?

Link to comment
Share on other sites

  • 0

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...

Link to comment
Share on other sites

This topic is now closed to further replies.