【问题标题】:URL - FileNotFoundException for image file in AndroidURL - Android 中图像文件的 FileNotFoundException
【发布时间】:2012-03-13 10:59:19
【问题描述】:

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

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...
  • @chinna_82 我在浏览器中打开了您的网址,它显示 404 - 找不到文件或目录。
  • @Padma Kumar 请立即查看。我更改了图片网址。请检查新网址

标签: java android exception


【解决方案1】:

在...

FileOutputStream f = new FileOutputStream(new File(root, FILENAME));

尝试将 FILENAME 替换为 fileURL。 另外,在哪一行抛出异常?这会有所帮助。

【讨论】:

  • 我正在从 Galaxy Nexus 进行测试,我无法找到导致错误的行......如果我将 FILENAME 更改为 fileUrl 我得到 /mnt/sdcard/sposter.smartag.my/images/caldophilus.jpg:open failed:ENOENT(no这样的文件或目录)
【解决方案2】:

试试这个:

  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)
  {

  }

【讨论】:

    【解决方案3】:

    试试下面的代码。它应该工作!

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

    【讨论】:

      【解决方案4】:

      我试试这个,它的工作正常。谢谢。

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

      【讨论】:

        【解决方案5】:

        立即尝试 -

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

        还有,看看here

        【讨论】:

          【解决方案6】:
          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); =========== remove this;
          
          c.connect();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多