Get Image from Url Unable to Decode Stream: fileNotFoundException(从 URL 获取图像无法解码流:fileNotFoundException)
问题描述
我知道这个问题被问了很多次,但我尝试了很多解决方案,但没有一个有效.
I know this issue as been ask many times but i've tried many solutions and no one works.
在 Android 上,我试图从 URL 获取图像以将其放入图像视图中.不幸的是,我收到以下错误:
On Android, I'm trying to get an image from URL to put it in an image view. Unfortunately, I get the following error :
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: http:/lorempixel.com/1920/1920/business (No such file or directory)
当我尝试从模拟器的浏览器访问此 URL 时,它可以工作.
When I try to reach this URL from the browser of the emulator, it works.
我已经尝试过以下解决方案:从url加载图片
I've already tried the following solutions : Load image from url
如何从 url 网站获取图片在android中的imageview中
如何从 url 为 imageView 设置图像
我的实际代码如下:
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView imgView;
public DownloadImage(ImageView imgView){
this.imgView = imgView;
}
@Override
protected Bitmap doInBackground(String... urls) {
return download_Image(urls[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null)
imgView.setImageBitmap(result);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
Log.e("CON ", "HTTP connection OK");
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap download_Image(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection("http://lorempixel.com/1920/1920/business");
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
在主要活动中,我的代码是:
In the main activity my code is :
ImageView img = (ImageView) findViewById(R.id.imageView);
DownloadImage download = new DownloadImage(img);
download.execute(url);
在我添加的清单中:
<uses-permission android:name="android.permission.INTERNET" />
我能做什么?
推荐答案
这很完美:
String urlPath = "http://anyWebsite.com/images/12"
Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(urlPath).getContent());
myImageView.setImageBitmap(bm);
这篇关于从 URL 获取图像无法解码流:fileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 URL 获取图像无法解码流:fileNotFoundException
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01