I need to create a service application which always looking and retrieve imaging file from if any new image taken from build in camera application.
1)My service needs to watching and taking the image from if new image was taken from build in camera application
how can we obtain the image path or image data in my service.
You can do that using CameraX. It is used to create your own camera app so you can do that by just omitting the preview part. Go to the below link for a small tutorial on cameraX https://developer.android.com/codelabs/camerax-getting-started#0 and you can just copy paste the code. For androidx.
This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.
You can learn more about an image or the objects around you with Google Lens. For example, you can take a photo of a plant and use it to search for info or other similar images.
manifest code:
<receiver android:name=".pictureReceiver" >
<intent-filter android:priority="10000" >
<action android:name="com.android.camera.NEW_PICTURE"/>
<data android:mimeType="image/*"/>
</intent-filter>
</receiver>
onReceive Code:
@Override
public void onReceive(Context arg0, Intent arg1) {
String selectedImageUri = arg1.getData().getPath();
Log.d("TAG", "Received new photo:::"+selectedImageUri );
Log.d("TAG","file Path"+getRealPathFromURI(arg1.getData(),arg0));
}
public String getRealPathFromURI(Uri contentUri,Context context)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
FYI it is recommended to listen to the official broadcast since ICS API level 14: http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_PICTURE. So filter for "android.hardware.action.NEW_PICTURE", and use "com.android.camera.NEW_PICTURE" in addition only if you need to support pre-ICS as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With