【问题标题】:I'm not getting the image from the url using Glide我没有使用 Glide 从 url 获取图像
【发布时间】:2018-01-04 18:15:17
【问题描述】:

这是我尝试过的代码。 我没有使用 Glide 获取 bmp 中的图像,当我在将图像转换为 byte[] 的方法中传递空 bmp 时,就会发生错误。
新的 AsyncTask() {

        @Override
        protected byte[] doInBackground(Void... voids) {
            Looper.prepare();
            byte[] byteimage = new byte[0];
            Bitmap bmp1;
            try {
                bmp= Glide.with(specificrecipe.this)//bmp is global
                        .load(url2img)
                        .asBitmap()
                        .into(100,100)
                        .get();
            byteimage=getBytes(bmp);//here I'm not getting the image in bmp
                //ByteArrayOutputStream stream = new ByteArrayOutputStream();
                //bmp.compress(Bitmap.CompressFormat.PNG, 0, stream);//null object refrence -> bmp
                //byteimage=stream.toByteArray();
                //byteimage1.add(0,byteimage);
                // initialize(bmp);
            }catch (final ExecutionException e){e.printStackTrace();}
            catch (final InterruptedException e){e.printStackTrace();}
            return byteimage;
        }

        //@Override
        //protected void onPostExecute(byte[] Bytearray){
        //byteimage1=Bytearray;
         //}
    }.execute();

【问题讨论】:

  • 如果要转换成位图。 Glide lib 会为你做这件事。
  • 看到在将 URL 传递给 Glide 后我没有收到 BMP 中的图像 null 异常发生。

标签: java android try-catch android-glide


【解决方案1】:

希望这对你有用

Bitmap theBitmap = Glide.
    with(this).
    load("http://....").
    asBitmap().
    into(100, 100). // Width and height
    get();

【讨论】:

  • 请提出另一种方法。我仍然没有收到来自 URL 的图像。
【解决方案2】:

//这是我从 URL 获取图像的方式,即使滑行无法从 URL 保存图像。

try {
       URL url = new URL(url2img);//url2img is a String
       bmp =BitmapFactory.decodeStream(url.openConnection().getInputStream());
       //bmp is global variable  
}catch (IOException e)
    {e.printStackTrace();}
    byteimage1=getBytes(bmp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多