Android PNG 到位图 --- SkImageDecoder::Factory 返回 null

Android PNG to Bitmap --- SkImageDecoder::Factory returned null

我正在尝试从 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 returned 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/directory: /storage/emulated/0/Pictures/Screenshots 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/file: /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 returned null 03-02 11:56:19.808 11581-11716/com.example.chilred_pc.myapplication D/skia: --- decoder->decode returned false

不是手动给 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。

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

> SystemClock.sleep(3000);

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