Java URL-Android中图像文件的FileNotFoundException

Java URL-Android中图像文件的FileNotFoundException,java,android,exception,Java,Android,Exception,我试图从特定URL获取图像,但它抛出FileNotFoundException。如果我尝试从浏览器打开url,我可以看到图像。请帮忙。下面是我的代码。谢谢 String fileURL = "http://sposter.smartag.my/images/KFC_Voucher.jpg"; String FILENAME = "caldophilus.jpg"; URL u = new URL(fileURL); HttpURLConnection c = (HttpURLConnection

我试图从特定URL获取图像,但它抛出
FileNotFoundException
。如果我尝试从浏览器打开url,我可以看到图像。请帮忙。下面是我的代码。谢谢

String fileURL = "http://sposter.smartag.my/images/KFC_Voucher.jpg";
String FILENAME = "caldophilus.jpg";
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
File root = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(new File(root, FILENAME));
InputStream x=c.getInputStream();
int size=x.available();
byte b[]= new byte[size];
x.read(b);
f.write(b);
f.flush();
f.close();
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();

尝试用fileURL替换文件名。 另外,在哪一行抛出异常?那会有帮助的。

试试这个:

  BufferedInputStream inputStream = null;
  OutputStream out = null;

  String fileName = null;
  String path = null;
  File savedFile = null;

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

      inputStream = new java.io.BufferedInputStream(connection.getInputStream());

      // Replace your save path here.
      File fileDir = new File("path/to/save");
      fileDir.mkdirs();
      savedFile = new File("path/to/save", fileName);
      out = new FileOutputStream(savedFile);

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

      long total = 0;

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

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

  }

请尝试下面的代码。它应该有用

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


public class DownloadManager {

    public static void downLoadImage(String imageURL, String destinationFileName) throws IOException {
        URL url = new URL(imageURL);
        InputStream inputStream = url.openStream();
        OutputStream outputStream = new FileOutputStream(destinationFileName);
        byte[] byteData = new byte[2048];
        int length;
        while((length=inputStream.read(byteData))!=-1) {
            outputStream.write(byteData, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }

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

我试过这个,效果很好。谢谢

String fileURL = "http://sposter.smartag.my/images/KFC_Voucher.jpg";
String FILENAME = "caldophilus.jpg";
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
File root = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(new File(root, FILENAME));
InputStream x=c.getInputStream();
int size=x.available();
byte b[]= new byte[size];
x.read(b);
f.write(b);
f.flush();
f.close();
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();
马上试试这个-

try {
            url = paths[0];
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            int length = connection.getContentLength();
            InputStream is = (InputStream) url.getContent();
            byte[] imageData = new byte[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);
                } else if ((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());
        }
试试看{
url=路径[0];
HttpURLConnection连接=(HttpURLConnection)url
.openConnection();
int length=connection.getContentLength();
InputStream为=(InputStream)url.getContent();
字节[]图像数据=新字节[长度];
int buffersize=(int)Math.ceil(长度/(双精度)100);
int=0;
int-read;
while(下载<长度){
if(长度<缓冲区大小){
read=is.read(图像数据,下载,长度);

}否则,如果((长度-下载)im从Galaxy Nexus测试,im无法找到导致错误的行…如果我将文件名更改为fileUrl,则我获得/mnt/sdcard/failed:enoint(没有此类文件或目录)你昨天发布了同样的问题。你没有得到正确的答案吗?昨天我使用webservice检索图像,现在我使用URL…@chinna_82我在borwser中打开了你的URL,它显示404-未找到文件或目录。@Padma Kumar,请现在检查。我更改了图像URL。请检查新的URL