Skip to content

机型适配 VideoUtil#ThumbTask 线程,在获取关键帧bitmap时出现 NPE #22

Description

@TopBigOne

android 系统 8.0, 锤子手机;
调用 Bitmap thumb = mediaMetadataRetriever.getFrameAtTime(nowPts, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC);
获取关键帧bitmap时,thumb 出现空指针,看了一下android 源码,对返回值bitmap的注解是 @nullable

public @Nullable Bitmap getFrameAtTime(long timeUs, @Option int option) {
        if (option < OPTION_PREVIOUS_SYNC ||
            option > OPTION_CLOSEST) {
            throw new IllegalArgumentException("Unsupported option: " + option);
        }

        return _getFrameAtTime(timeUs, option, -1 /*dst_width*/, -1 /*dst_height*/, null);
    }

所以,调用getFrameAtTime() 要对返回值做一下非空判断

  @Override
        public void run() {
            long nowPts = start;
            MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
            mediaMetadataRetriever.setDataSource(path);
            while (nowPts < end) {
                String dstJpg = VideoUtil.getThumbJpg(context, path, nowPts);
                Bitmap thumb = mediaMetadataRetriever.getFrameAtTime(nowPts, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC);
                // add  by guangya to avoid bitmap  NPE bitmap ---start
                if(thumb==null){
                    Log.e(TAG, "ThumbTask: thumb is NULL");
                    return;
                }
                // add  by guangya to avoid bitmap  NPE bitmap ---end
                thumb = Bitmap.createScaledBitmap(
                        thumb,
                        (int) (context.getResources().getDisplayMetrics().density * 60),
                        (int) (context.getResources().getDisplayMetrics().density * 60),
                        true);
                nowPts += 1000000L;
                saveBitmapFile(thumb, dstJpg);
            }
            mediaMetadataRetriever.release();
            if (callback != null) {
                callback.handleMessage(null);
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions