我想绘制位图转一个网址?

时间:2016-09-07 09:43:11

标签: android

@Override
        protected void onDraw(Canvas canvas) {
            String url = "http://dizay.net/files/yuklenenler/4kuAJ0egT90HnVbJz5czHgsPAL3Z4V8TgLsZC37jm8TdsvO9nZeFg1yNgD0jN.png";
            Bitmap image, temp, x;
            temp = BitmapFactory.decodeResource(getResources(), R.drawable.duvar);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

             x=getBitmapFromURL(url);
          //  Bitmap over = BitmapFactory.decodeResource(getResources(), getBitmapFromURL(url).describeContents());
            image = Bitmap.createScaledBitmap(x, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            temp = BitmapFactory.decodeResource(getResources(), R.drawable.golge);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            temp = BitmapFactory.decodeResource(getResources(), R.drawable.supurge);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);


            temp = BitmapFactory.decodeResource(getResources(), R.drawable.hali);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            temp = BitmapFactory.decodeResource(getResources(), R.drawable.tablo);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            temp = BitmapFactory.decodeResource(getResources(), R.drawable.cicek);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);


            temp = BitmapFactory.decodeResource(getResources(), R.drawable.mobilya);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            temp = BitmapFactory.decodeResource(getResources(), R.drawable.sehpa);
            image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
            canvas.drawBitmap(image, 0, 0, null);

            super.onDraw(canvas);


        }

    }

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

}

错误

09-07 05:10:54.913 1888-1888/com.example.dogushur.canvas E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.dogushur.canvas, PID: 1888
                                                                           android.os.NetworkOnMainThreadException
                                                                               at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                               at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
                                                                               at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                               at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                               at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                                                                               at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
                                                                               at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
                                                                               at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
                                                                               at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
                                                                               at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
                                                                               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
                                                                               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
                                                                               at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
                                                                               at com.example.dogushur.canvas.MainActivity.getBitmapFromURL(MainActivity.java:106)
                                                                               at com.example.dogushur.canvas.MainActivity$Ciz.onDraw(MainActivity.java:60)
                                                                               at android.view.View.draw(View.java:16178)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15174)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15169)
                                                                               at android.view.View.draw(View.java:15948)
                                                                               at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
                                                                               at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
                                                                               at android.view.View.draw(View.java:16181)
                                                                               at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
                                                                               at android.view.View.updateDisplayListIfDirty(View.java:15174)
                                                                               at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
                                                                               at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
                                                                               at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
                                                                               at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
                                                                               at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                            at android.view.Choreographer$FrameDis
09-07 05:15:55.288 1888-1888/com.example.dogushur.canvas I/Process: Sending signal. PID: 1888 SIG: 9

1 个答案:

答案 0 :(得分:0)

你不能在主线程中做网络的东西。您必须创建异步操作:

class Check extends AsyncTask<String, Integer, Bitmap b> {

    @Override
    protected Bitmap doInBackground(String... src) {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        setBitmap(myBitmap);//for access in main class
        return myBitmap;
        //do connections
    }
}

并致电代码:

new Check().execute("https://example.com");

您可以禁用StrictMode,但这是一个非常糟糕的主意。首先启用它是有原因的。

如果 您决定禁用StrictMode,请执行以下操作:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

尽管如此,这样做会导致类似这样的事情:Android freeze screen 20 seconds after execute a HTTP Post