Android PNG到Bitmap --- SkImageDecoder :: Factory返回null

Mad*_*din 7 android bitmap bitmapfactory android-bitmap

我正在尝试从我的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);

}
Run Code Online (Sandbox Code Playgroud)

--- 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();
}
Run Code Online (Sandbox Code Playgroud)

有人知道如何解决这个问题吗?也许是因为我的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/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返回null 03-02 11:56:19.808 11581-11716/com.example.chilred_pc.myapplication D/skia:--- decoder-> decode返回false

Mad*_*din 1

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

> SystemClock.sleep(3000);
Run Code Online (Sandbox Code Playgroud)

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