简体   繁体   中英

Returning Bitmap from object URL

I want to write a method that returns Bitmap image from object URL. But it gives NetworkOnMainThreadException error.

Image.java:

package gc.x.models;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.annotations.SerializedName;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import gc.x.R;


/**
 * Created by ASUS on 4.10.2016.
 */
public class Image {


    public String albumId;
    public String id;
    public String title;
    public String url;
    public String thumbnailUrl;


    public Bitmap getBitmapFromURL() {//get bitmap of thumbnail from thumbnailurl
        try {
            URL url = new URL(this.thumbnailUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }


}

I don't want to simply show bitmap image from url because code structure is different.To prevent confusion, I did not share my all codes. I just want to write a method that gets bitmap from object's thumbnailurl. Later, I will use this method. I get images from http://jsonplaceholder.typicode.com/

You cannot perform this network task in the main thread, you have to write this inside the Async task.

write this code inside the class

private class AsyncClass extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        getBitmapFromURL();
    }

    @Override
    protected void onPostExecute(String result) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

and call it like this

new AsyncClass().execute("");

Your issue is that you are using main thread for network operation... Moreover I'd suggest to use image load framework to achieve that goal ie use Glide .. with one line you have that bitmap. You don't need to solve mainThread .. because Glide takes care of that for you.Just concentrate on your code.

Bitmap theBitmap = Glide.with(this).
                load("http://...."). // or url 
                asBitmap().
                into(100, 100). // Width and height
                get();

Glide: https://github.com/bumptech/glide

you need to perform this operation in a background thread. See my answer for code File Upload to server issues in Android

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