简体   繁体   中英

Get Bitmap image from Url

I want to get Bitmap image from 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;
}

But app stop in this line:

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

Error:

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.ja va: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.Fr agmentManagerImpl.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)

As exception says you can't make network calls in the Main(UI) thread. So you need to wrap your code into runnable/thread and call it. Could look like this:

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

Usage:

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();

Or even better method : use a library for loading images .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM