Android - Downloaded image from url is broken - java

I'm using URL to download and save image in Android. but sometimes saved image is broken and half of them get blacked.
for example see this:
I use this code:
URL _url = new URL(url);
_url.openConnection().setReadTimeout(10000);
_url.openConnection().setConnectTimeout(10000);
InputStream is = _url.openStream();
OutputStream os = new FileOutputStream(filePath + fileName);
byte[] b = new byte[1024];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();

Related

How to read data from file in server in byte array

I want to read a file from a server and get the data of it.
I have written following piece of code.
URL uurl = new URL(this.m_FilePath);
BufferedReader in = new BufferedReader(new InputStreamReader(uurl.openStream()));
String str;
while ((str = in.readLine()) != null) {
text_file=text_file+str;
text_file=text_file+"\n";
}
m_byteVertexBuffer=text_file.getBytes();
But i am getting wrong result! If I read data from a string, I get m_bytevertexbuffer length=249664.
Now when I read a local file into the bytearray then i get m_bytevertexbuffer length=169332.
FileInputStream fis = new FileInputStream(VertexFile);
fis.read(m_byteVertexBuffer);
ByteBuffer dlb=null;
int l=m_byteVertexBuffer.length;
I want the same data in bytebuffer from a server and also from a local file!
If the server sends a header Content-Length: 999 you could allocate a new byte[999].
URL url = new URL("http://www.android.com/");
URLConnection urlConnection = url.openConnection();
int contentLength = urlConnection.getContentLength();
// -1 if not known or > int range.
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//if (contentLength >= 0) {
// byte[] bytes = new byte[contentLength];
// in.read(bytes);
// m_byteVertexBuffer = bytes;
//} else {
ByteArrayOutputStream baos;
byte[] bytes = new byte[contentLength == -1 ? 10240 : contentLength];
for (;;) {
int nread = in.read(bytes, 0, bytes.length);
if (nread <= 0) {
break;
}
baos.write(bytes, 0, nread);
}
m_byteVertexBuffer = baos.toByteArray();
//}
} finally {
urlConnection.disconnect();
}
In the general case you would use only the code of the else-branch. But still, one the presence of a valid content-length it is usable.

save images locally - canvas

The code right down to this saving photos from a HTML page is working.
String
nameimage,
srcfoto;
nameimage = vs.getValor("NAMEIMAGE");
srcfoto = vs.getValor("img").toString();
URL url = new URL(srcfoto);
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("C:\\img\\ " + nameimage + ".png");
fos.write(response);
fos.close();
vs.setReturnOK("Image save!");
return vs.getReturnOK();
My problem is that I need to download the following canvas src..
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAABkCAYAAABwx8J9AAAeKElEQVR4Xu2de+h2S1XHNfqvLLCLlZW/MjAyTyiR1Sk5kHTT7hfs/hJRpySlLEvKhILSPJ0g0mNF9RbdMel+kcwjncIEuxsJnfqJ3ROR6vSHRLU+x2fZas7sPWv2nv1c9vt9YPE+7++ZmT3zXWvmO7NmzeyHP0wfISAEhIAQEAJC4OIRePjFt0ANEAJCQAgIASEgBB4mQpcRCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HShRTRACQkAICAEhIEKXDQgBISAEhIAQ2AECIvQdKFFNEAJCQAgIASEgQpcNCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HShRTRACQkAICAEhIEKXDQgBISAEhIAQ2AECIvQdKFFNEAJCQAgIASEgQpcNCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HShRTRACQkAICAEhIEKXDQgBISAEhIAQ2AECIvQdKFFNEAJCQAgIASEgQpcNCAEhIASEgBDYAQIi9B0oUU0QAkJACAgBISBClw0IASEgBISAENgBAiL0HSjxAprwOVbHVyys579YvqeYvHFh/p5sT7LEr5/J8Cb77fEmD/QUqrRCQAgIgWMgsFdCjwSyZBB+VwP/PpMnH5TwVfbvjx6+l7+N0lNJXDUSfKo97FWdD6yV80Ir43kT5azFrlbsORP6u1iFX2JyZyeuS+yq8xFKfiYIHKvPn0lzZ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spkEH66yc+asH/srnDvdxn9RNx600dvDoN++d7zilreOcEa0T7Kz+IFNngQiMRnRTryxSzUoxe7GjZuU9gshMyEtPy43ml3uR0TXeiOS237w3Xl/SIeP/TnebBjidPa/lxrt/52IQjsldDdbdy61GFKTXSKpWVsnddJwTsubWCA8cCrkgBa7Yj1XWK2cxh71PtbrGAP+OEZflTnufb9pRMD45K6lHmcJPyq0fstwZtN2A9lNcPd2Y814dgZpM/gWBuky3IZcFlV4T6O7nbSsTf66SbcHudl4mLGW8FqNL5JrNdWetPTbtoI+VAvnt8KTKMNo9rnuPm2B+f6wQJs3D3segBP7OGmSdymKLFf+v9e7KaeA2FzbLFsC/g+xsS3Dl5r36MtkY+LighOpO0Qud/3XnuWewSYhIERgY1PMImeDLZ/OL4a75bHvphAZq+qXoqn8p0hAnsk9NJtvOStTWvKOEZeVjJcYuIf3Me1FU2mLkvc7KUptzDmGZDrlFv6GF3DXb24e8HOXb2c479eWAHKZB/V20UxpeuVv12ZuGvW00CyGf3EqvWmp36fHAp4pX3vIcu17avBShvAzLdfalsfC9Uxm60Xu1Yd3J68LegdAp+6rrXss9hf60gepO44UZ9aPy9ti77IPQg9em61Vb9fCAJ7JPQLgV7VFAJCQAgIASEwDgER+jgsVZIQEAJCQAgIgZMhIEI/GfR6sBAQAkJACAiBcQiI0MdhqZKEgBAQAkJACJwMARH6yaDXg4WAEBACQkAIjENAhD4OS5UkBISAEBACQuBkCIjQTwa9HiwEhIAQEAJCYBwCIvRxWKokISAEhIAQEAInQ0CEfjLo9WAhIASEgBAQAuMQ+F9sgNYKIgd0wQAAAABJRU5ErkJggg==
Only by accepting this src, example /image.png and don't data:image
In debug...
I was reading a bit on this question but did not understand, and do not know if this is the problem Convert Data URI to File then append to FormData
any help is welcome...

How to write files to an SD Card from the internet?

An example would be a simple image.
I have tried so many things and it just refuses to work despite making a whole lot of sense.
What I've done so far is I'm able to grab 25 pictures and add them to
/sdcard/app name/sub/dir/filename.jpg
They all appear there according to the DDMS but they always have a filesize of 0.
I'm guessing it's probably because of my input stream?
Here's my function that handles the downloading and saving.
public void DownloadPages()
{
for (int fileC = 0; fileC < pageAmount; fileC++)
{
URL url;
String path = "/sdcard/Appname/sub/dir/";
File file = new File(path, fileC + ".jpg");
int size=0;
byte[] buffer=null;
try{
url = new URL("http://images.bluegartr.com/bucket/gallery/56ca6f9f2ef43ab7349c0e6511edb6d6.png");
InputStream in = url.openStream();
size = in.available();
buffer = new byte[size];
in.read(buffer);
in.close();
}catch(Exception e){
}
if (!new File(path).exists())
new File(path).mkdirs();
FileOutputStream out;
try{
out = new FileOutputStream(file);
out.write(buffer);
out.flush();
out.close();
}catch(Exception e){
}
}
}
It just keeps giving me 25 files in that directory but all of their file sizes are zero. I have no idea why. This is practically the same code I've used in a java program.
PS...
If you're gonna give me a solution... I've already tried code like this. It doesn't work.
try{
url = new URL(urlString);
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
System.out.println("Now downloading File: " + filename.substring(0, filename.lastIndexOf(".")));
while ((count = in.read(data, 0, 1024)) != -1){
fout.write(data, 0, count);
}
}finally{
System.out.println("Download complete.");
if (in != null)
in.close();
if (fout != null)
fout.close();
}
}
Here's an image of what my directories look like
http://oi48.tinypic.com/2cpcprm.jpg
A bit change to your second option, try it as following way,
byte data[] = new byte[1024];
long total = 0;
int count;
while ( ( count = input.read(data)) != -1 )
{
total += count;
output.write( data,0,count );
}
This one is different in while statement while ((count = in.read(data, 0, 1024)) != -1)
Using Guava something like this should work:
String fileUrl = "xxx";
File file = null;
InputStream in;
FileOutputStream out;
try {
Uri url = new URI(fileUrl);
in = url.openStream();
out = new FileOutputStream(file)
ByteStreams.copy(in, out);
}
catch (IOException e) {
System.out.println(e.toString());
}
finally {
in.close();
out.flush();
out.close();
}

No number of bytes received in java

I would like to determine the number of bytes downloaded from the following working URL connection:
I have following code to implement:
.......
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream(); // throws an IOException
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
FileOutputStream fos = new FileOutputStream("C:\\Picture.jpeg");
int read =0;
byte[] bytes = new byte[1024];
while((read = dis.read(bytes)) != -1)
{
fos.write(bytes, 0, read);
}
System.out.println(read + " byte(s) copied");
The output from the last line is as follows:
Opening connection to http://www.xyz.com//Picture.jpeg...
Copying image resource (type: application/jpeg, modified on: 02/02/2010 4:19:21 AM)...
-1 byte(s) copied
What is the error of my code. please help me
int read =0;
int reddit = 0;
byte[] bytes = new byte[1024];
while((read = dis.read(bytes)) != -1)
{
fos.write(bytes, 0, read);
reddit += read;
}
//your read variable must have the value -1 at this point
System.out.println(reddit + " byte(s) copied");
int totalBytes = 0;
...
while((read = dis.read(bytes)) != -1)
{
totalBytes += read;
fos.write(bytes, 0, read);
}

Android - file download problem - incomplete file

I have checked many code snippets, tried with and without buffer and I can't get to download whole file to SD card. The code I use currently is:
try {
url = new URL("http://mywebsite.com/directory/");
} catch (MalformedURLException e1) { }
String filename = "someKindOfFile.jpg"; // this won't be .jpg in future
File folder = new File(PATH); // TODO: add checking if folder exist
if (folder.mkdir()) Log.i("MKDIR", "Folder created");
else Log.i("MKDIR", "Folder not created");
File file = new File(folder, filename);
try {
conn = url.openConnection();
is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
is.close();
} catch (IOException e) { }
This code creates directory on SD card but downloads only 77 bytes of files. What might be the problem?
The error here is that he was writing the count variable converted to byte datatype instead of the bytes read from the input stream (those should be stored in a temporary byte[] buffer via bis.read(buffer))
The proper code block should be:
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(file);
int current = 0;
byte[] buffer = new byte[1024];
while ((current = bis.read(buffer)) != -1) {
fos.write(buffer, 0, current);
}
fos.close();
is.close();

Categories