Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top