获取FileNotFoundException,即使文件在android中

时间:2014-02-11 16:30:01

标签: android android-asynctask

我正在尝试从链接中获取图像,这是我的工作:

public class FetchImage extends AsyncTask<String, Void, Drawable> {

public static Drawable LoadImageFromWeb(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (IOException e) {
        Log.e("HATA", "THERE is an error", e);
        return null;
    }
}

@Override
protected Drawable doInBackground(String... params) {
    String url = params[0];
    return LoadImageFromWeb(url);
}

@Override
protected void onPostExecute(Drawable drawable) {

}

}

对于网址http://www.marketimyilmazlar.com/image/cache/data/INDIRIM/ao/Pril_TR_MAX_750ml_Lemon_189227_print_1772H_1772W-500x500.png,我成功获取了图片。但是,当我尝试url http://www.marketimyilmazlar.com/image/cache/data/cay /Ncapkırmızı-500x500.jpg时,它会给我一个filenotfoundexception。这是stactrace:

02-11 18:22:15.475: E/HATA(16015): THERE is an error
02-11 18:22:15.475: E/HATA(16015): java.io.FileNotFoundException:     http://www.marketimyilmazlar.com/image/cache/data/cay/Ncapkırmızı-500x500.jpg
02-11 18:22:15.475: E/HATA(16015):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
02-11 18:22:15.475: E/HATA(16015):  at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1018)
02-11 18:22:15.475: E/HATA(16015):  at java.net.URLConnection.getContent(URLConnection.java:199)
02-11 18:22:15.475: E/HATA(16015):  at java.net.URL.getContent(URL.java:447)
02-11 18:22:15.475: E/HATA(16015):  at com.example.barcodescanner.FetchImage.LoadImageFromWeb(FetchImage.java:18)
02-11 18:22:15.475: E/HATA(16015):  at com.example.barcodescanner.FetchImage.doInBackground(FetchImage.java:30)
02-11 18:22:15.475: E/HATA(16015):  at com.example.barcodescanner.FetchImage.doInBackground(FetchImage.java:1)
02-11 18:22:15.475: E/HATA(16015):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-11 18:22:15.475: E/HATA(16015):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-11 18:22:15.475: E/HATA(16015):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-11 18:22:15.475: E/HATA(16015):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-11 18:22:15.475: E/HATA(16015):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-11 18:22:15.475: E/HATA(16015):  at java.lang.Thread.run(Thread.java:841)

真的很奇怪。是不是因为链接中的一些土耳其字符,如“ı”,而不是“我”?如果是这样,我该如何解决?

由于

编辑:当我在浏览器中打开followink链接时,我意识到了另一件事,

http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot 7-500x500.jpg

我看到链接发生变化并变为:

http://www.marketimyilmazlar.com/image/cache/data/DUZELTME/dvm/screenshot%207-500x500.jpg

那么,为什么会发生这种情况,为什么我不能在尝试获取图像时使用第一个链接?

2 个答案:

答案 0 :(得分:2)

你对“ı”土耳其人的角色是正确的:如果看看你的答案,你会发现Stackoverflow无法自动识别给你带来问题的网址。

使用Firefox我可以访问您的网址,但是当我使用开发者工具检查网址时,网址为http://www.marketimyilmazlar.com/image/cache/data/cay/Ncapk%C4%B1rm%C4%B1z%C4%B1-500x500.jpg 请注意,非ASCII字符是转义的。

编辑1:

要访问包含非ascii字符的网址,您需要在发出请求之前使用特殊字符对网址部分进行编码:URL encoding in Android

答案 1 :(得分:1)

我想你可以在这里找到答案:

Invalid URI with Chinese characters (Java)