Okay I have a thread that wants to access a .jpg file, which is before written to the internal storage by another thread:
ImageSaverThread ist = new ImageSaverThread(mPreview.getmCameraInstance());
ist.start();
File file1 = ist.getOutputMediaFile(1);
try {
ist.join();
this.sleep(1); // This is only for debugging!
} catch (InterruptedException e) {
e.printStackTrace();
Log.d("IST ERROR", "ImageSaverThread did not finish!");
return;
}
File mediaStorageDir = new File(String.valueOf(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
File file = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + "TEST" + ".jpg");
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(file));
short[][] image = getByteImage(bitmap);
However, when I run my code, I get an error because my bitmap is a null
Object. The image to be loaded is obviously not yet available. When I put a Thread.sleep(50)
instead of the ist.join()
, everything works fine, as i wait 50 ms for the ImageSaverThread
to finish.
Any help here?
EDIT
This is what ImageSaverThread does:
public void run() {
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.d("FAIL", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("FAIL", "Error accessing file: " + e.getMessage());
}
camera.stopPreview();
camera.startPreview();
}
};
mCamera.takePicture(null, null, mPicture);
}