我想从网址(http://www.bbc.co.uk/blogs/spanish/ravsberg_442x330jpg.jpg)获取远程JPG文件并将其放入Bitmap。
我正在尝试使用此代码:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {e.printStackTrace();}
return null;
}
但代码失败了:InputStream input = connection.getInputStream();
我遇到了这个例外:
03-15 16:08:40.171: WARN/System.err(6526): java.io.FileNotFoundException: http://www.bbc.co.uk/blogs/spanish/ravsberg_442x330jpg.jpg
03-15 16:08:40.186: WARN/System.err(6526): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
03-15 16:08:40.186: WARN/System.err(6526): at com.Magazine.Util.getBitmapFromURL(Util.java:69)
03-15 16:08:40.194: WARN/System.err(6526): at com.Magazine.magazine.getMiniature(magazine.java:32)
03-15 16:08:40.194: WARN/System.err(6526): at com.Magazine.MainMenu.generatePortraitGrid(MainMenu.java:141)
03-15 16:08:40.202: WARN/System.err(6526): at com.Magazine.MainMenu.onCreate(MainMenu.java:113)
03-15 16:08:40.210: WARN/System.err(6526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-15 16:08:40.210: WARN/System.err(6526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-15 16:08:40.210: WARN/System.err(6526): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-15 16:08:40.210: WARN/System.err(6526): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-15 16:08:40.217: WARN/System.err(6526): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-15 16:08:40.217: WARN/System.err(6526): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 16:08:40.217: WARN/System.err(6526): at android.os.Looper.loop(Looper.java:123)
03-15 16:08:40.225: WARN/System.err(6526): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-15 16:08:40.225: WARN/System.err(6526): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:08:40.225: WARN/System.err(6526): at java.lang.reflect.Method.invoke(Method.java:521)
03-15 16:08:40.225: WARN/System.err(6526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-15 16:08:40.225: WARN/System.err(6526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-15 16:08:40.233: WARN/System.err(6526): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在Manifest中查看您的Internet权限,并检查网址是否正确。 我使用了这个页面的函数“urlImageToBitmap(String urlImage)”: http://www.tutorialandroid.com/basico/como-pasar-de-drawable-a-bitmap-y-descargar-imagen-de-internet-a-bitmap/ 我希望它可以帮到你!
答案 1 :(得分:1)
看看这个解决方法:https://stackoverflow.com/a/4510418/1216540 它对我有用。
祝你好运