Android PNG to Bitmap --- SkImageDecoder::Factory returned null
Asked Answered
C

2

7

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

Cynic answered 2/3, 2016 at 10:22 Comment(6)
Make sure you have give permission for WRITE_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE in androidmanifest.xml . Please post your error log.Ine
what do you get when you do int fileSize = file.length() ? what is the value of fileSizeTrutko
I did write it in androidmanifest.xml already.Cynic
int fileSize = file.length() is 0. So I really dont know why? If I take a look on my explorer it is 0. On my smartphone it is 430 kb. I tried with another picture and it do not work alsoCynic
My bad! With an another PNG(Size 430kb) it works.Cynic
No, in the end it doesent workCynic
C
1

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

Cynic answered 4/3, 2016 at 15:14 Comment(0)
I
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.

Ine answered 2/3, 2016 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.