views:

90

answers:

2

Below is the code to write to a file after downloading an image from the web. But somehow, it's just stuck while writing to File Stream. No Exception at all. It just stays on while loop then displays force close popup. it happens 2 or 3 out of 10.

private void saveImage(String fullPath) {
  File imgFile = new File(fullPath);
  try {
    if(imgFile.exists()) imgFile.delete();

    URL url = new URL(this.fileURL);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection)connection;
    InputStream in = httpConnection.getInputStream();

    FileOutputStream fos = new FileOutputStream(imgFile);
    int n = 0;
    while((n = in.read()) != -1) {
        fos.write(n);
    }
    fos.flush();
    fos.close();
    in.close();

  }
  catch(IOException e) {
    imgFile.delete();
    Log.d("IOException Thrown", e.toString());
  }
}

When I check the file written, it's always stuck when it writes till 4576byte. (Original image size is over 150k) Is there anybody who can help me on this issue?

A: 
private void saveImage(String fullPath) {
  File imgFile = new File(fullPath);
  try {
    if(imgFile.exists()) imgFile.delete();

/**    URL url = new URL(this.fileURL);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection)connection;
    InputStream in = httpConnection.getInputStream();

    FileOutputStream fos = new FileOutputStream(imgFile);
    int n = 0;
    while((n = in.read()) != -1) {
        fos.write(n);
    }
    fos.flush();
    fos.close();
    in.close();***/

 FileOutputStream fos = new FileOutputStream(imgFile);

URL url = new URL(this.fileUR);
Object content = url.getContent();
InputStream in =  (InputStream) content;
Bitmap bitmap = BitmapFactory.decodeStream(in);
bitmap.compress(CompressFormat.JPEG,80, fos);
fos.flush();
fos.close();
in.close();


  }
  catch(IOException e) {
    imgFile.delete();
    Log.d("IOException Thrown", e.toString());
  }
}
Jorgesys
Just change your compress Format and Quality in bitmap.compress(,,) function. =)
Jorgesys
Thanks. I tested it a couple of times after I switched the code. So far, it looks ok. What do you think the problem of my code? Shouldn't I use 'while' loop for writing stream?
juniano
Thanks again, Jorgesys.. I used your first suggestion and it's working great.
juniano
Hi Juniano your welcome!
Jorgesys
Hi Jorgesys. Did you solve the 'process is bad' issue? I found your question about it and no update after. I am having the same problem but could find the solution. I know this is not the right place to ask you the question but this is only place I can contact you. :)
juniano
hi Juniano why are you getting "process is bad" exception , are u using this code in an android widget???
Jorgesys
Yes I am using this code in an app widget. but I don't think that this code causes the exception... but I am not sure... thought it's just a separate question. Well... is there any common solution for the 'process is bad' problem?
juniano
Hi Jorgesys.. do you still have this issue?
juniano
A: 

Hi Juniano answering your second question, yes, you can use the while loop but you need 2 things more.

  URL url = new URL(this.fileURL);
  URLConnection connection = url.openConnection();
  HttpURLConnection httpConnection = (HttpURLConnection)connection;

  InputStream in = httpConnection.getInputStream();

   BufferedInputStream myBis = new BufferedInputStream(in);

1) create a ByteArrayBuffer to store your InputStream

     ByteArrayBuffer myBABuffer = new ByteArrayBuffer(50);
                              int current = 0;
                              while ((current = myBis.read()) != -1) {
                                      myBABuffer.append((byte) current);
                              }    

    FileOutputStream fos = new FileOutputStream(imgFile);

2) create the image with the stored Bytes.

fos.write(myBABuffer.toByteArray());    
fos.flush();
fos.close();
in.close();

Jorgesys

Jorgesys