从Url

时间:2015-07-15 14:00:00

标签: java android url bitmap

我想从Bitmap获取Url图片:

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;
}

但app停在这一行:

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

错误:

  

致命的例外:主要       android.os.NetworkOnMainThreadException               在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)               在libcore.net.http.HttpConnection。(HttpConnection.java:70)               在libcore.net.http.HttpConnection。(HttpConnection.java:50)               在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:341)               在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)               在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)               在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)               在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)               在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)               在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)               在com.example.sivolotskiy.multiexpro.ui.EditFragment.setImageInFrames(EditFragment.java:161)               在com.example.sivolotskiy.multiexpro.ui.EditFragment.startAfterView(EditFragment.java:119)               在com.example.sivolotskiy.multiexpro.ui.EditFragment_.onViewChanged(EditFragment_.java:202)               at org.androidannotations.api.view.OnViewChangedNotifier.notifyViewChanged(OnViewChangedNotifier.java:41)               在com.example.sivolotskiy.multiexpro.ui.EditFragment_.onViewCreated(EditFragment_.java:71)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)               在android.app.BackStackRecord.run(BackStackRecord.java:635)               在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)               在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:426)               在android.os.Handler.handleCallback(Handler.java:615)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:4745)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)               在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:2)

异常表示您无法在Main(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