图片来源网址的Andr​​oid来源、网址、图片、oid

2023-09-12 22:59:32 作者:一生放荡

我想从为此我使用的是网络服务将图像设置:

I am trying to set an image from a web service for which I am using:

private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... arg0) {
        Bitmap b = null;
        try {
            b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent());
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }
}

和试图获取它像

final ImageView imgicon = (ImageView) convertView.findViewById(R.id.imgicon); 
    new FetchImageTask() {
        @Override
        protected void onPostExecute(Bitmap result) {

            if (result != null) {
                imgicon.setImageBitmap(result);

            }
        }
    }.execute("Url/images/"+bitmapname);

不过,这并不显示它也没有任何错误。任何的猜测?

But it doesn't display it nor any error. Any guess?

推荐答案

试试这个:

public Bitmap getBitmapFromURL(String src) {
    try {
        java.net.URL url = new java.net.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;
    }
}

有关OutOfMemoryIssue使用:

for OutOfMemoryIssue USe:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

    return resizedBitmap;
}