【问题标题】:Android PNG to Bitmap --- SkImageDecoder::Factory returned nullAndroid PNG to Bitmap --- SkImageDecoder::Factory 返回 null
【发布时间】:2016-06-15 03:18:45
【问题描述】:

我正在尝试从我的 Environment.getExternalStorageDirectory() 加载屏幕截图 并尝试将其转换为位图

 public void onPictureTaken(String path) throws IOException {

    String photoPath = filepath + "/" + path;; //UPDATE WITH YOUR OWN JPG FILE

    File directory = new File (filepath);
    File file = new File(directory, path);

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(photoPath, options);

    // Calculate inSampleSize
    options.inSampleSize = 4;
    options.inJustDecodeBounds = false;
    BitmapFactory.decodeFile(photoPath, options);

}

--- SkImageDecoder::Factory 返回 null

这是我调用 onPictureTaken 的函数:

 private void observeSceenshot(){
    filepath = Environment.getExternalStorageDirectory()
            + File.separator + Environment.DIRECTORY_PICTURES
            + File.separator + "Screenshots";
    Log.d(TAG, filepath);

    FileObserver fileObserver = new FileObserver(filepath, FileObserver.CREATE) {
        @Override
        public void onEvent(int event, String path) {
            Log.d(TAG, event + " " + path);
            try {
                onPictureTaken(path);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    fileObserver.startWatching();
}

有人知道如何解决这个问题吗?也许是因为我的 png 太大(1280x720)? 我也尝试了这个解决方案,结果相同: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

编辑:这是日志

03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/DBG_com.example.chilred_pc.myapplication.ObserveScreenshots: 256 Screenshot_2016-03-02-11-56-19.png 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/目录:/storage/emulated/0/Pictures/Screenshots 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/文件:/storage/emulated/0/Pictures/Screenshots/Screenshot_2016-03-01-16-38-08.png 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/fileSize: 35061 03-02 11:56:19.807 11581-11716/com.example.chilred_pc.myapplication D/skia: --- SkImageDecoder::Factory 返回 null 03-02 11:56:19.808 11581-11716/com.example.chilred_pc.myapplication D/skia: --- 解码器->解码返回假

【问题讨论】:

  • 确保您在 androidmanifest.xml 中拥有 WRITE_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 的权限。请发布您的错误日志。
  • 当您执行int fileSize = file.length() 时会得到什么? fileSize 的值是多少
  • 我确实已经在androidmanifest.xml中写过了。
  • int fileSize = file.length() 为 0。所以我真的不知道为什么?如果我看一下我的资源管理器,它是 0。在我的智能手机上它是 430 kb。我尝试了另一张图片,它也不起作用
  • 我的错!使用另一个 PNG(大小 430kb)它可以工作。

标签: android bitmap android-bitmap bitmapfactory


【解决方案1】:

我认为解决问题的方法是截图需要几秒钟才能创建图像。所以我尝试停止系统几秒钟,现在它正在工作。

> SystemClock.sleep(3000);

但最后我使用了另一种方法,如下所示: Detect only screenshot with FileObserver Android

【讨论】:

    【解决方案2】:

    代替手动给options.inSampleSize = 4的值,计算InSampleSize如下:

    ...
    int reqWidth=100; // give your requested width
    int reqHeight=100;// give your requested height
    options.inSampleSize = calculateSampleSize(options,reqWidth,reqHeight);
    ...
    
    public static int calculateSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
    
        final int width = options.outWidth;
        final int height = options.outHeight;
        int inSampleSize = 1;
    
        if (width > reqWidth || height > reqHeight) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }
    

    计算在您所指的同一 like 上给出的 InSampleSize。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2012-06-01
      相关资源
      最近更新 更多