Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to load an screenshot from my Environment.getExternalStorageDirectory() and try to convert it to bitmap

 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

Here is my function which calls 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();
}

Does anybody know how to solve the problem? Maybe because my png is to big(1280x720)? I also tried this solution with the same result: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Edit: Here is the log

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

like image 727
Maddin Avatar asked Mar 02 '16 10:03

Maddin


1 Answers

I think the solution to the problem is that the screenshot needs a few seconds to create a image. So I tried to stop the system for a few seconds and now it is working.

> SystemClock.sleep(3000);

But in the end I used another method, which is shown here: Detect only screenshot with FileObserver Android

like image 177
Maddin Avatar answered Sep 19 '22 12:09

Maddin