2012-02-23 5 views
4

特定のURLから画像を取得しようとしていますが、FileNotFoundExceptionがスローされます。ブラウザからURLを開こうとすると、画像が表示されます。助けてください。以下は私のコードです。ありがとう。で...URL - Androidの画像ファイルのFileNotFoundException

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

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

を見てみましょうあなたは昨日同じ質問を掲載しています。あなたは適切な答えを得られないのですか? – Android

+0

昨日私は画像を取得するためにwebserviceを使いましたが、現在はURLを使用しています... –

+0

@ chinna_82私は404を表示しているborwserにあなたのURLをオープンしました - ファイルまたはディレクトリが見つかりません。 –

答えて

2

私はこれを試しても問題ありません。ありがとう。

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

fileURLでFILENAMEを交換してみてください。 また、どのラインで例外がスローされますか?それが助けになるだろう。

+0

Galaxy Nexusからimのテストがありません。どの行がエラーを引き起こすかを知るために....そして、もし私がfileUrlにFILENAMEを変更すると、/mnt/sdcard/http://sposter.smartag.my/images/caldophilus.jpg:openが失敗しました:ENOENT(そのようなファイルはありませんまたはディレクトリ) –

3

これを試してみてください:

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

    } 
1

は、以下のコードを試してみてください。それは動作するはずです!

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

一度にこれを試してみてください -

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

関連する問題