Wednesday, June 20, 2012

android::CameraHardwareSec::takePicture() : capture already in progress error

Today, One of the Android devices keep getting crashed while taking a picture in a TimerTask. I was keep getting this error


E/CameraHardwareSec(75): virtual android::status_t android::CameraHardwareSec::takePicture() : capture already in progress
E/PanicImageActivity(1750): java.lang.RuntimeException: takePicture failed


problem is before you call this method again in the TimerTask run method. you need to make sure that there is one not in progress. Some devices cameras are very slow on processing picture frames.


private final Semaphore mutex = new Semaphore(1);



try {

mutex.acquire();


mCamera.takePicture(null, null, new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera camera) {
try {
             // Do something here
              mutex.release();
         }

         catch(Throwable t) {
          mutex.release();
         }

});
}
catch (Throwable t) {

 }

Tuesday, June 19, 2012

How to check whether Android Media Scanner is running ?

Here is the code


    public static final boolean isMediaScannerScanning(final ContentResolver cr) {
        boolean result = false;
        final Cursor cursor = query(cr, MediaStore.getMediaScannerUri(), new String[] { MediaStore.MEDIA_SCANNER_VOLUME }, null,
                null, null);
        if (cursor != null) {
            if (cursor.getCount() == 1) {
                cursor.moveToFirst();
                result = "external".equals(cursor.getString(0));
            }
            cursor.close();
        }
        return result;
    }

How to open a local file in Android browser?

Important thing here is you have to use file:// + path


 private String genLocalPageUrl(String fileName)  {
    return "file://" + this.getFilesDir() + "/" +  fileName ;
 }

How to open browser link in new tab or not open in Android?

This is not something I was not looking for but I just found it. I thought it will be helpful for someone out there.

key point here is putExtra("create_new_tab", false);

Intent  intent  = new Intent("android.intent.action.VIEW", null);
intent.addCategory("android.intent.category.BROWSABLE");
intent.setComponent(paramComponentName);
intent.putExtra("create_new_tab", false); // true or false
intent.putExtra("com.android.browser.application_id", paramComponentName.getPackageName());
startActivity(intent);