在URL上获取位图图像[关闭]

问题描述 投票:1回答:2

我想通过获取bitmap从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();

}

从URL运行getBitmap时它会崩溃。

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

}

android bitmap
2个回答
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;
        }

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'

最后用上下文调用这个公共静态函数

© www.soinside.com 2019 - 2024. All rights reserved.