1

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);
}
PKlumpp
  • 4,913
  • 8
  • 36
  • 64
  • why do you need a separate thread, if you're waiting for it anyway? – AdamSkywalker Dec 12 '15 at 20:29
  • that is not really answering the question why the join does not work – PKlumpp Dec 12 '15 at 20:30
  • Possible duplicate of [BitmapFactory.decodeFile returns null even image exists](http://stackoverflow.com/questions/3388898/bitmapfactory-decodefile-returns-null-even-image-exists) – OleGG Dec 12 '15 at 20:32
  • @ZerO yes, that's why I comment your question and do not post an asnwer – AdamSkywalker Dec 12 '15 at 20:34
  • I was told to use those two threads. So basically, I have no choice to change this. – PKlumpp Dec 12 '15 at 20:35
  • what is the code of ImageSaverThread ? – Gerard Rozsavolgyi Dec 12 '15 at 20:39
  • @GerardRozsavolgyi added the code – PKlumpp Dec 12 '15 at 20:43
  • might be the startPreview() method that cause the problem. According to the docs : "After calling this method, you must not call startPreview() or take another picture until the JPEG callback has returned." (see http://developer.android.com/reference/android/hardware/Camera.html#takePicture%28android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback%29) – Gerard Rozsavolgyi Dec 12 '15 at 20:54

0 Answers0