0

I am wondering, is there a way my app can display a message as soon as the user capture an photo using the default camera app?

So basically, when the user opens the default camera app, and take a picture, I want my app to display a TOAST or a message (and also has access to this image offcourse).

Is there any way I can do that?

Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250
  • send the code you tried – Invader Nov 22 '13 at 03:44
  • I don't know where to start. I searched a lot but couldn't find questions similar – Snake Nov 22 '13 at 03:48
  • This has been asked quite a few times on SO and the short answer is no, not unless your app is always running in the background and listening for changes on the camera folders and/or media store. Even then a change to either one doesn't necessarily mean that a new photo was taken. – MH. Nov 22 '13 at 03:48

2 Answers2

2

try using this.

// method to take picture from camera
protected void takepicture() {
    // TODO Auto-generated method stub

    String fileName = "temp.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    mCapturedImageURI = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    values.clear();
    startActivityForResult(intent, 5);
}

and in activiyt result:

    // on activity result we store captures image in images
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 5) {

            String[] projection = { MediaStore.Images.Media.DATA };
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(mCapturedImageURI, projection,
                    null, null, null);
            int column_index_data = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            image_path = cursor.getString(column_index_data);
            Log.e("path of image from CAMERA......******************.........",
                    image_path + "");
            // bitmap = BitmapFactory.decodeFile(image_path);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            bitmap = BitmapFactory.decodeFile(image_path, options);
                         Toast.makeText(getApplicationcontext, "photo taken", 1000)
                .show();




        }

    }
Invader
  • 679
  • 3
  • 10
  • Thank you for your answer but this will open/capture the image from within my app. I don't want that. What I want is that when the user start the default camera app and they take a picture, somehow my app is being notified of this so I can display a msg – Snake Nov 22 '13 at 03:50
0

You can use FileObserver like below:-

FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA") { // set up a file observer to watch this directory on sd card
            @Override
        public void onEvent(int event, String file) {
            if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
                Log.d(TAG, "File created [" + android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA/" + file + "]");
                fileSaved = "New photo Saved: " + file;
            }
        }
    };
    observer.startWatching(); // start the observer

You can also use the CameraEventReciver:-

Create a receiver for your code say CameraEventReciver and in that you can implement your code like:-

public class CameraEventReciver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Cursor cursor = context.getContentResolver().query(intent.getData(),      null,null, null, null);
    cursor.moveToFirst();
    String image_path = cursor.getString(cursor.getColumnIndex("_data"));
    Toast.makeText(context, "New Photo is Saved as : -" + image_path, 1000).show();
      }
    }

And in Android Manifest you just have to take some permissions and register your reciever with intent filter and appropriate action for image capture also make your receiver android enabled like:-

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.CAMERA" />

   <receiver
        android:name="com.android.application.CameraEventReciver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="com.android.camera.NEW_PICTURE" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </receiver> 

for more information take a look:-

broadcast receiver won't receive camera event

This may help you.

Community
  • 1
  • 1
bashu
  • 1,710
  • 12
  • 16