在URL上获取位图图像

时间:2019-02-17 09:12:30

标签: android bitmap

我想通过获取bitmap

从URL加载图像。

如何在url中加载图片,因为在此图片上仍然出现错误。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getActionBar().hide(); 
    setContentView(R.layout.prof);
    json_page1 = getIntent().getExtras().getString("json_page1");
    json_string1 = getIntent().getExtras().getString("json_data");
    fname = getIntent().getExtras().getString("fname");
    lname = getIntent().getExtras().getString("lname");
    pid = getIntent().getExtras().getString("pid");
    email = getIntent().getExtras().getString("email");
    cn = getIntent().getExtras().getString("cn");
    //Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();

    JSONObject obj;
    try {
        obj = new JSONObject(json_string1);
        JSONArray arr = obj.getJSONArray("data");
        //JSONObject obj1 = arr.getJSONObject(0);
        //fname = obj1.getString("fname");
         pfname= arr.getJSONObject(0).getString("fname");
         plname= arr.getJSONObject(0).getString("lname");
         pemail= arr.getJSONObject(0).getString("email");
         pcontact= arr.getJSONObject(0).getString("contact");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    yn = (TextView)findViewById(R.id.yname);
    yem = (TextView)findViewById(R.id.ycontact);
    ycn = (TextView)findViewById(R.id.yemail);
    pn  = (TextView)findViewById(R.id.pname);
    pem = (TextView)findViewById(R.id.pemail);
    pcn = (TextView)findViewById(R.id.pcontact);
    prid = (TextView)findViewById(R.id.pid);

    yn.setText("Name: "+fname+" "+lname);
    yem.setText("Email: "+email);
    ycn.setText("Contact: "+cn);
    pn.setText("Name: "+pfname);
    pem.setText("Email: "+pemail);
    pcn.setText("Contact: "+pcontact);
    prid.setText("Professor ID: "+pid);

   iv1 = (ImageView)findViewById(R.id.studp);

    new Thread(new Runnable(){
        public void run() {
            src = "https://c7.alamy.com/comp/D8MF25/red-rose-flower-detailed-imge-D8MF25.jpg";
            bitmap = getBitmapFromURL(src);
            iv1.setImageBitmap(bitmap);
        }
        }).start();

}

运行getBitmapFromURL时会崩溃。

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) {
        // Log exception
        return null;
    }
}

}

2 个答案:

答案 0 :(得分:1)

尝试

 @Override
        protected String doInBackground(String...imageUrl) {
            try {
                URL url = new URL(imageUrl);
                Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

答案 1 :(得分:0)

按如下所述更改getBitmapFromUrl方法,此外,您还必须添加glide的依赖项:

public static Bitmap getBitmapFromURL(String src,Context mContext) {
   Glide.with(mContext).asBitmap().load(src).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
             Bitmap myBitmap=resource;
             return myBitmap;
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
             //do what you want
            }
        });
}

依赖:

implementation 'com.github.bumptech.glide:glide:4.8.0'

最后使用上下文调用此公共静态函数