【问题标题】:My Image from Server becomes NULL我来自服务器的图像变为 NULL
【发布时间】:2017-06-05 16:27:30
【问题描述】:

我正在使用 PHP 将我的图像上传到 MySQL 服务器,并且上传良好。

但是下载的时候问题就来了。我已经测试了我从中下载图像的链接,但在 ImageView 中显示它是一项繁琐的任务。它在 LogCat 窗口中没有显示任何错误。位图变为空(我使用bitmap==null 进行了比较)

我的 DoInBackground 和 OnPostExecute

        @Override
        protected Bitmap doInBackground(String... params) {
            String id = params[0];
            String add = "http://dakshansh.com/mybooks/image/getImage.php?id=" + imageId;
            Log.d("Image","Link = "+add);
            URL url = null;
            Bitmap image = null;
            try {
                url = new URL(add);
                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return image;
        }

        @Override
        protected void onPostExecute(Bitmap b) {
            super.onPostExecute(b);
            //loading.dismiss();
            if(b==null){
                Log.d("Image","Image is null");
            }
            Log.d("Image","On onPostExecute()");
            image.setImageBitmap(b);
            Log.d("Image", "Image Set");
            new ReadJSONFeedTask1().execute("http://dakshansh.com/mybooks/books/getmybooks.php?posterid="+id);


        }

如果您需要,我会发布更多内容。 可以看图here

【问题讨论】:

  • 只是一个友好的建议。使用 Picasso 或 Glide 处理图像。
  • 同意 Anton 的观点,以后会省去很多麻烦。但是,回到主题,您是否在代码中收到 IOException?
  • url.openConnection().getInputStream() 流中有什么内容?你那里有数据吗?
  • 它适用于其他活动。所以我不认为我应该改变
  • 是的@Shark,我没有得到任何异常

标签: php android mysql bitmap android-asynctask


【解决方案1】:

使用 glide 会为您节省大量时间

 ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

   Glide.with(this).load("your_url").into(imageView);

更多here

【讨论】:

  • Glide 是什么。我看不懂github。如果可以的话,给出完整的代码。滑翔变红
  • 将此添加到您的 gradle compile 'com.github.bumptech.glide:glide:3.7.0' ,使用 libaray 的最基本方法是 Glide.with(MyActivity.this).load("your_url").into(your_image_view);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多