代码之家  ›  专栏  ›  技术社区  ›  Tamal Dev

如何在imageview中显示php图像?

  •  0
  • Tamal Dev  · 技术社区  · 7 年前

    我怎样才能下载那张照片并展示出来。

    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 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);?>
    

    如何解决?

    5 回复  |  直到 7 年前
        1
  •  0
  •   Jaimin Prajapati    7 年前

    无需为图像加载编写代码。

    对毕加索的依赖是

    compile 'com.squareup.picasso:picasso:2.5.2'
    
    Picasso.with(context).load(your_image_url).into(imageView);
    
        2
  •  0
  •   Pranav MS    7 年前

    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
  •  0
  •   Mahesh Vayak    7 年前

    Click Here .

    <uses-permission android:name="android.permission.INTERNET" />
    
        4
  •  0
  •   Nguyễn Trung Hiếu    7 年前

    您应该使用库滑动: 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
  •  0
  •   Alireza    7 年前

    你可以使用 Picasso Glide 用于从服务器获取图像并加载到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" />