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
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01