【问题标题】:Receiving image from server in android在android中从服务器接收图像
【发布时间】:2015-07-28 15:39:04
【问题描述】:

我有一个 android 应用程序,它向服务器请求图像,并且应该在 ImageView 中显示该请求图像。图像存储在图像文件夹中,其路径存储在MySQL 数据库中。我正在使用php 进行服务器端脚本。 Android 应用程序对特定图像的请求到image.php 文件,该文件将请求图像所在文件夹的路径 从数据库中获取并以 json 格式返回。 现在我的问题是:

  1. 仅此路径就足以在 android 应用程序中显示图像吗?还是应用程序应该先下载该图像才能显示它?

  2. 我在 SO 的某处看到您可以将图像编码为字符串并将该字符串返回给 android 应用程序并将该字符串解码回图像?这是一种有效的方法吗?

这是一个更笼统的问题,所以任何事情都会受到赞赏。

【问题讨论】:

    标签: php android mysql image


    【解决方案1】:
    1. 是的,您必须下载图像才能显示它,但您不必永远存储它,您可以使用BitmapFactory 解码ByteArray 以获得图像的Bitmap。或者使用像 Picasso 这样的库来“缓存”您的图像,就像另一个答案中建议的那样。

    This question 讨论了许多关于如何使用一些库在 Android 上下载图像的方法。或者您可以尝试使用原生方法,如下所示:

    public class LoginActivity extends Activity implements OnClickListener {
    
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.login);
    
       this.findViewById(R.id.userinfo_submit).setOnClickListener(this);
       // Verify Code
       LinearLayout view = (LinearLayout) findViewById(R.id.txt_verify_code);
       view.addView(new VerifyCodeView(this));
    
       // show The Image
       new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
       .execute(“http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png”);
       }
    
      public void onClick(View v) {
        startActivity(new Intent(this, IndexActivity.class));
        finish();
      }
    
      private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
    
        public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
        }
    
        protected Bitmap doInBackground(String… urls) {
          String urldisplay = urls[0];
          Bitmap mIcon11 = null;
          try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
            Log.e(“Error”, e.getMessage());
            e.printStackTrace();
          }
          return mIcon11;
        }
    
        protected void onPostExecute(Bitmap result) {
          bmImage.setImageBitmap(result);
        }
      }
    }
    

    此代码取自here

    1. 您可以在服务器上将图像编码为 Base64 String,然后在 Android 上对其进行解码。

    要对其进行解码,请尝试以下操作:

    byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
    Bitmap bm;
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;
    bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
    
    // Now do whatever you want with the Bitmap.
    

    您可以查看Bitmaphere 的文档。

    但老实说,您只是在流程中添加了另一个步骤,并因此浪费了处理器周期。我想直接下载图片效率更高。

    有关Base64 类的更多信息,请参阅docs

    【讨论】:

    • 感谢您对我的问题的准确回答!
    【解决方案2】:

    试试毕加索。这很容易和简单

        Picasso.with(context)
           .load("http://i.imgur.com/DvpvklR.png")//Your image link url
           .into(imageView);//Your imageView
    

    完整的文档可以在here找到。

    【讨论】:

      【解决方案3】:

      如果您有图片的 URL,我建议您使用 Glide 或 Picasso。

      滑翔:

      https://github.com/bumptech/glide

      Glide.with(myFragment)
              .load(url)
              .centerCrop()
              .placeholder(R.drawable.loading_spinner)
              .into(myImageView);
      

      毕加索:

      http://square.github.io/picasso/

      Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
      

      如果您使用的是 Android Studio,可以通过将它们作为依赖项添加到应用的 Gradle 文件中,轻松地将其中任何一个库添加到您的 Android 项目中。

      将此添加到 Glide 的 Gradle 文件中的依赖项中:

      compile 'com.github.bumptech.glide:glide:3.6.0'
      

      将此添加到毕加索的 Gradle 文件中的依赖项:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-11
        • 2015-09-18
        相关资源
        最近更新 更多