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

时间:2016-03-02 10:22:13

标签: android bitmap android-bitmap bitmapfactory

我正在尝试从我的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 /图片/截图   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 return false

2 个答案:

答案 0 :(得分:1)

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

> SystemClock.sleep(3000);

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

答案 1 :(得分:0)

Instead of given manual value to options.inSampleSize = 4, calculate InSampleSize like below:

...
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;
}

Calculate InSampleSize given on same like which you are referring.