6 posts in this topic

Posted

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();

Share this post


Link to post
Share on other sites

Posted

Anyone?

Share this post


Link to post
Share on other sites

Posted

Dont let me down Neowin! :)

Share this post


Link to post
Share on other sites

Posted

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?

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.