繁体   English   中英

使用滑行时,位图返回null

[英]Bitmap is returning null when using glide

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

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(String path){

    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,则它将从内部类中调用。 声明为最终决定。

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

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

您应该在onResourceReady()主体内使用它。

或使用Glide:

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

您需要将此内容包装在asynctask中的函数中并执行。 请参阅this answer ,该内容说明了在后台加载以及将位图设置为重写的onPostExecute()的imageview的方法。

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

我的解决方案:

编辑:位图位图= 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM