Skip to content Skip to sidebar Skip to footer

Url - Filenotfoundexception For Image File In Android

I'm trying to get a image from particular URL but it throwsFileNotFoundException. If I try to open the url from my browser, i can see the images. Please help. Below is my code. Tha

Solution 1:

i try this and its work fine. Thanks.

URL url = new URL(fileURL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/caldophilus.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();

Solution 2:

Try this:

BufferedInputStreaminputStream=null;
  OutputStreamout=null;

  StringfileName=null;
  Stringpath=null;
  FilesavedFile=null;

  try
  {
      // Replace your URL here.URLfileURL=newURL("http://enter.your.url.here");
      URLConnectionconnection= fileURL.openConnection();
      connection.connect();

      inputStream = newjava.io.BufferedInputStream(connection.getInputStream());

      // Replace your save path here.FilefileDir=newFile("path/to/save");
      fileDir.mkdirs();
      savedFile = newFile("path/to/save", fileName);
      out = newFileOutputStream(savedFile);

      byte buf[] = newbyte[1024];
      int len;

      longtotal=0;

      while ((len = inputStream.read(buf)) != -1)
      {
        total += len;
        out.write(buf, 0, len);
      }

      out.close();
      inputStream.close();
  }
  catch (Exception)
  {

  }

Solution 3:

Try the below code. It should work!

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;


publicclassDownloadManager {

    publicstaticvoiddownLoadImage(String imageURL, String destinationFileName)throws IOException {
        URLurl=newURL(imageURL);
        InputStreaminputStream= url.openStream();
        OutputStreamoutputStream=newFileOutputStream(destinationFileName);
        byte[] byteData = newbyte[2048];
        int length;
        while((length=inputStream.read(byteData))!=-1) {
            outputStream.write(byteData, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }

    publicstaticvoidmain(String[] args)throws IOException {
        StringimageURL="http://sposter.smartag.my/images/KFC_Voucher.jpg";
        StringdestinationFileName="C:/Users/sarath_sivan/Desktop/caldophilus.jpg";
        downLoadImage(imageURL, destinationFileName);
    }
}

Solution 4:

Try this at once -

try {
            url = paths[0];
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            int length = connection.getContentLength();
            InputStream is = (InputStream) url.getContent();
            byte[] imageData = newbyte[length];
            int buffersize = (int) Math.ceil(length / (double) 100);
            int downloaded = 0;
            int read;
            while (downloaded < length) {
                if (length < buffersize) {
                    read = is.read(imageData, downloaded, length);
                } elseif ((length - downloaded) <= buffersize) {
                    read = is.read(imageData, downloaded, length
                            - downloaded);
                } else {
                    read = is.read(imageData, downloaded, buffersize);
                }
                downloaded += read;
                publishProgress((downloaded * 100) / length);
            }
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
                    length);
            if (bitmap != null) {
                Log.i(TAG, "Bitmap created");
            } else {
                Log.i(TAG, "Bitmap not created");
            }
            is.close();
            return bitmap;
        } catch (MalformedURLException e) {
            Log.e(TAG, "Malformed exception: " + e.toString());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.toString());
        }

And, just take a look at here

Solution 5:

In ...

FileOutputStreamf=newFileOutputStream(newFile(root, FILENAME));

Try replacing FILENAME with fileURL. Also, at which line is the exception thrown? That would help.

Post a Comment for "Url - Filenotfoundexception For Image File In Android"