Reputation: 81
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
Upvotes: 7
Views: 2055
Reputation: 81
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
Upvotes: 1
Reputation: 25312
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.
Upvotes: 0