【问题标题】:How to show a php image in imageview?如何在 imageview 中显示 php 图像?
【发布时间】: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


【解决方案1】:

无需为图像加载编写代码。 有著名的android应用程序图像加载库,毕加索将负责调用重试,还将图像保存在应用程序缓存中。所以,不是每次都下载同一张图片。

对毕加索的依赖是

compile 'com.squareup.picasso:picasso:2.5.2'

Picasso.with(context).load(your_image_url).into(imageView);

【讨论】:

    【解决方案2】:

    您可以从如下网址加载图片

    URL url = new URL("image full path");
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bmp);
    

    另一种加载图片的方式

    private Bitmap bmp;
    
       new AsyncTask<Void, Void, Void>() {                  
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    InputStream in = new URL(IMAGE_URL).openStream();
                    bmp = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                   // log error
                }
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                if (bmp != null)
                    imageView.setImageBitmap(bmp);
            }
    
       }.execute();
    

    请在此处reference。 请试一试

    【讨论】:

      【解决方案3】:

      关注此链接Click Here

      确保您在 AndroidManifest.xml 中设置了以下权限以访问互联网。

      <uses-permission android:name="android.permission.INTERNET" />
      

      【讨论】:

      • 好的...酷点击上面的链接。
      【解决方案4】:

      您应该使用库 Glide:https://github.com/bumptech/glide

      导入

      compile 'com.github.bumptech.glide:glide:4.1.1'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'
      

      用途:

      Glide.with(this).load("http://getbloodbd.org/image.php?no=4").into(iv);
      

      或者如果你想获得位图:

      Glide.with(this).asBitmap().load("http://getbloodbd.org/image.php?no=4").listener(new RequestListener<Bitmap>() {
              @Override
              public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                  return false;
              }
      
              @Override
              public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                  iv.setImageBitmap(resource);
                  return false;
              }
          });
      

      【讨论】:

        【解决方案5】:

        你可以使用 PicassoGlide 从服务器获取图像并加载到 ImageView 中,有很多选项。

        • 毕加索的样本

          Picasso.with(context)
          .load(url)
          .centerCrop()
          .placeholder(R.drawable.user_placeholder)
          .error(R.drawable.user_placeholder_error)
          .into(imageView);
          
        • 滑翔示例

          GlideApp
          .with(context)
          .load(url)
          .centerCrop()
          .placeholder(R.drawable.loading_spinner)
          .into(imageView);
          

        不要忘记在 AndroidManifest.xml 文件中添加互联网权限。

        <uses-permission android:name="android.permission.INTERNET" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-23
          • 1970-01-01
          • 1970-01-01
          • 2020-12-16
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多