gpt4 book ai didi

java - 从 Url 获取位图图像

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:14 24 4
gpt4 key购买 nike

我想从 Url 获取 Bitmap 图像:

Bitmap mage = getBitmapFromUrl(urlPhotoInFrameFirst);

public static Bitmap getBitmapFromUrl (String uri){
URL url = null;
try {
url = new URL(uri);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return image;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

但是应用程序停止在这一行:

Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

错误:

FATAL EXCEPTION: main android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) at libcore.net.http.HttpConnection.(HttpConnection.java:70) at libcore.net.http.HttpConnection.(HttpConnection.java:50) at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) at com.example.sivolotskiy.multiexpro.util.BitmapLoader.getBitmapFromUrl(BitmapLoader.java:33) at com.example.sivolotskiy.multiexpro.ui.EditFragment.setImageInFrames(EditFragment.java:161) at com.example.sivolotskiy.multiexpro.ui.EditFragment.startAfterView(EditFragment.java:119) at com.example.sivolotskiy.multiexpro.ui.EditFragment_.onViewChanged(EditFragment_.java:202) at org.androidannotations.api.view.OnViewChangedNotifier.notifyViewChanged(OnViewChangedNotifier.java:41) at com.example.sivolotskiy.multiexpro.ui.EditFragment_.onViewCreated(EditFragment_.java:71) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) at android.app.BackStackRecord.run(BackStackRecord.java:635) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)

最佳答案

作为异常(exception),您不能在主 (UI) 线程中进行网络调用。所以你需要将你的代码包装到 runnable/thread 中并调用它。可能看起来像这样:

public static class GetBitmapTask implements Runnable {

private final String uri;
private final Callback callback;

public GetBitmapTask(String uri, Callback callback) {
this.uri = uri;
this.callback = callback;
}

@Override public void run() {
try {
URL url = new URL(uri);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
callback.onFinish(bmp);
} catch (IOException e) {
callback.onError(e);
}
}

public interface Callback{
void onFinish(Bitmap bitmap);
void onError(Throwable t);
}
}

用法:

new Thread(new GetBitmapTask("", new GetBitmapTask.Callback() {
@Override public void onFinish(Bitmap bitmap) {
//here is your bitmap
}

@Override public void onError(Throwable t) {
//here you have to handle error
}
})).start();

或者更好的方法:use a library for loading images .

关于java - 从 Url 获取位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com