图片没有显示图像视图视图、图像、图片

2023-09-04 04:27:16 作者:谁念西风独自凉

我想上的ImageView设置的图像,但图像不显示。

I am trying to set image on imageview but image is not show.

我从JSON数据读取图像的URL,然后尝试在ImageView的设置,但我的形象是不可见的。没有任何异常发生。 这是我的code

I am reading image url from json data and then trying to set it on ImageView but my image is not visible. No any exception occur. Here is my code

HotelList.class

HotelList.class

static final String TAG_DISHIMAGEURL = "dishimageurl";
......
String imageUrl = dishResult.getString(TAG_DISHIMAGEURL);
map.put(TAG_DISHIMAGEURL, imageUrl);
.....
dishimageurl1 = hm.get(TAG_DISHIMAGEURL).toString();
 intent.putExtra("background", dishimageurl1);

HotelDetails.class

HotelDetails.class

......
String dishimageurl = bundle.getString("background");
Bitmap bimage=  getBitmapFromURL(dishimageurl);
    imageView.setImageBitmap(bimage);
....   
public Bitmap getBitmapFromURL(String src) {
try {
    URL url = new URL(src);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    Toast.makeText(this, "showing image", Toast.LENGTH_LONG).show();
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
} catch (IOException e) {
    Toast.makeText(this, "showing exception", Toast.LENGTH_LONG).show();
    return null;
}

}

我不明白发生了什么与此code。没有任何异常,但我的形象是不可见的。照片 请给我任何引用。

I don't understand what happen with this code. No any exception but my image is not visible. Please give me any reference.

推荐答案

请点击下面$ C $下获取图像的URL,并显示到ImageView的。

Please Use below code for get image from url and display into imageview.

public class image extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=https://m.xsw88.com/allimgs/daicuo/20230904/7268.png");

        RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1);
        Drawable d=new BitmapDrawable(bitmap);
        mRlayoutLogin.setBackgroundDrawable(d);
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }
}