【发布时间】:2023-04-07 04:07:01
【问题描述】:
我正在尝试使用此代码,但它不起作用。 我怎样才能下载该图片并显示它。
我的java代码
private ImageView iv;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showprofile);
iv = (ImageView) findViewById(R.id.virus);
bitmap = getBitmapFromURL("http://getbloodbd.org/image.php?no=4");
iv.setImageBitmap(bitmap);}
public 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 (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
我的xml代码是
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.getbloodbd.getbloodbd.ShowprofileActivity"
tools:showIn="@layout/app_bar_showprofile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/virus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我的 php 代码在 webview 方法中工作正常,但图像视图不起作用,但我需要图像视图方法。
<?php include 'config.php';header("Content-type: image/gif" );$no=$_GET['no']; $dn = mysql_query('select image from images where no="'.$no.'"'); $dnn = mysql_fetch_array($dn); $data = $dnn['image'];echo base64_decode($data);?>
如何解决?
【问题讨论】:
-
给出完整的网址
标签: java php android image imageview