【问题标题】:Bitmap is returning null when using glide使用滑行时位图返回 null
【发布时间】:2017-08-04 20:58:44
【问题描述】:

我一直在试图弄清楚为什么位图在 SimpleTarget 内给了我一个大小值,但在 SimpleTarget 之外就为空。

private Bitmap getImage(String path){

    final Bitmap[] bitmaps = new Bitmap[1];

    Glide.with(getApplicationContext())
            .load(path)
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    bitmaps[0] = resource;
                    Log.v(TAG, "bitmap: " + bitmaps[0].getByteCount());// Returns a value as expected.
                }
            });
        Log.v(TAG, "bitmap2: " + bitmaps[0].getByteCount());// throws a null object reference.
        return bitmaps[0];
    }

编辑:AsyncTask 方法。

私有位图getImage(字符串路径){

    final Bitmap[] bitmaps = new Bitmap[1];

    new AsyncTask<Void, Void, Void>() {
        Bitmap tmp;

        @Override
        protected Void doInBackground(Void... params) {
            try {
                tmp = Glide.with(getApplicationContext())
                        .load("http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available.jpg")
                        .asBitmap()
                        .into(-1,-1)
                        .get();
            } catch (final ExecutionException | InterruptedException e) {
                Log.e(TAG, e.getMessage());
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void dummy) {
            if (null != tmp) {
                // The full bitmap should be available here
                bitmaps[0] = tmp;
                Log.v(TAG, "bitmap: " + bitmaps[0].getHeight());
                Log.d(TAG, "Image loaded");
            };
        }
    }.execute();

        Log.v(TAG, "bitmap2: " + bitmaps[0].getHeight());// throws a null object reference.
        return bitmaps[0];
    }

编辑:添加了与问题相关的日志。

08-04 19:35:03.687 5183-5183/com.example.comics V/com.example.comics.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
08-04 19:35:03.709 5183-5183/com.example.comics V/com.example.comics.backend.services.BackgroundService: bitmap: 537

编辑:如果位图未定义为最终的,它会喊出从内部类访问。声明为 final。

【问题讨论】:

    标签: android bitmap android-glide


    【解决方案1】:

    您正在创建对参数的引用并在其函数之外使用它,当您离开该函数时 bitmap[0] 将为空。

    您必须使用以下代码创建位图的副本:
    bitmaps[0] = resource.copy(resource.getConfig(), true);

    您应该在 onResourceReady() 正文中使用它。

    或者使用 Glide:

    bitmaps[0] = Glide.with(getApplicationContext())
            .load(path)
            .asBitmap()
            .into ....
            .get();`
    

    您需要将其包装在 asynctask 的函数中并执行它。 请参阅this answer,它解释了在后台加载并将位图设置为覆盖onPostExecute() 内的图像视图。

    【讨论】:

    • 好吧,我不知道。我确实按照你的建议尝试了。第一个示例产生相同的问题,第二个示例我在 logcat 中收到一个错误,通知我需要在后台线程上运行。我在 Service 类的 Java 线程中执行这些操作。
    • 对不起万娜我已经尝试了你所说的一切。我总是得出同样的结果。在我的研究过程中,我确实注意到有些人确实建议避免使用 bitmaps[0] = Glide.with(getApplicationContext()) 方法,因为它会降低性能。您提到的另外 2 个并不完全符合预期。
    • 异步任务方法给了我同样的问题。 onPostExecute() 可以记录.V 大小。在异步之外,我只得到了一个空异常。
    • 您无法返回我首先考虑的资源之一。这是因为 SimpleTarget 更像是一个内部方法,而不是想要返回位图的方法。
    【解决方案2】:

    我终于让它发挥作用了。 第一个位图必须在全局工作区中声明。 (就像一个对象)。 第二个 Log.v 必须在 if 语句中,因为正在使用 Restful 客户端服务下载数据库。它并不总是获得预期的数据。

    我的解决方案:

    编辑: 位图bitmap = null;

    private Bitmap getImage(final String path){
    
        new AsyncTask<Void, Void, Void>() {
    
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    bitmap = Glide.with(getApplicationContext())
                            .load(path)
                            .asBitmap()
                            .into(-1,-1)
                            .get();
                } catch (final ExecutionException | InterruptedException e) {
                    Log.e(TAG, e.getMessage());
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void dummy) {
            }
        }.execute();
    
        if(bitmap != null) {
            Log.v(TAG, "bitmap2: " + bitmap.getHeight());
        }
            return bitmap;
        }
    

    但是,在前 30 张图像之后执行时出现错误。我猜测它是因为从 for 循环中调用了该方法,并导致了令人讨厌的错误列表。

    08-04 20:20:21.191 12350-12457/com.example.comics E/com.example.comics.backend.services.BackgroundService: java.io.IOException: Request failed 404: Not Found
    08-04 20:20:21.658 12350-12457/com.example.comics E/com.example.comics.backend.services.BackgroundService: java.io.IOException: Request failed 404: Not Found
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2015-02-22
      • 1970-01-01
      • 2021-04-27
      • 2012-01-16
      • 2021-01-20
      • 1970-01-01
      相关资源
      最近更新 更多