Open device camera only after firing intent for opening camera - java

I have a scenario in which I fire a intent to open camera through following code.
if(isCameraInUse())
return;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageFile = getOutputFromCamera();
Uri tempuri = Uri.fromFile(mImageFile);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
startActivityForResult(i, 0);
Now if I have more than one camera app, then I see all of them as option to open. But I want device camera only to be opened. Is this possible? Is it possible to fire an intent specify which app should catch that intent?

I guess you should create an object of android.hardware.Camera. And simply calling open method of it will do the work for you.
Calling open() method on that Camera camera = Camera.open(); it will create a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.
You may use this in your code before
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Hope this helps. I believe that you have added these in your application's manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

If you want to launch a specific application use this code. I hope it will solve your problem.
PackageManager pm = getActivity().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("camera package name");
startActivity(intent);

You need to call setPackage on the Intent
i.setPackage("package.name");
startActivityForResult(i, 0);

Instead of this:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Do this:
Intent i = new Intent("com.android.camera");
Call the camera by the package name think it should help.

Related

Java solution for "startActivityForResult(Intent,int) in Fragment has been deprecated" when opening external URL?

My app contains a simple Fragment that is used to open external web pages, with:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivityForResult(chooserIntent, RC_OPEN_URL);
And, when the result is returned (in my Fragment's onActivityResult(...)), the Fragment is popped off the backstack.
But, I'm now getting the new deprecation warning:
startActivityForResult(Intent,int) in Fragment has been deprecated
I've read the corresponding Getting a result from an activity documentation, but I'm not sure how to adapt the example they provide for my particular case.
I have discovered the ActivityResultContracts.StartActivityForResult class, but can't figure out how to pass my chooserIntent to it.
All the online examples for the class seem to be in Kotlin and I've not had any joy trying to decompile them to Java. So a Java example of how to use the new registerForActivityResult() method to open an external URL would be much appreciated.
There's no reason to use startActivityForResult() at all for createChooser() - you can use startActivity and run your code from onActivityResult() immediately after you call to startActivity:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivity(chooserIntent);
// Do your code from onActivityResult() here
However, if you really want to use the Activity Result API, then you can directly adapt the examples in the documentation, replacing the example GetContent contract with the StartActivityForResult contract:
// Create this as a variable in your Fragment class
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
// Do your code from onActivityResult
}
});
private void triggerChooser(Uri externalUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
mLauncher.launch(chooserIntent);
}
Below answer may help to someone.... But this is not readymade solution to above question.
I have faced lot of issue when I try to get result from activity to fragment.
Finally I found below solution.
In side the fragment, I created ActivityResultLauncher.
var myActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult<Intent, ActivityResult>(
ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback<ActivityResult> {
// ToDO:
if (it.resultCode == AppCompatActivity.RESULT_OK) {
}
}
) as ActivityResultLauncher<Intent>
And when I start the activity I used below code.
myActivityResultLauncher.launch(myIntent)

Android No Activity found - STILL_IMAGE_CAMERA

I am getting the following exception in my android app.
No Activity found to handle Intent { act=android.media.action.STILL_IMAGE_CAMERA (has extras) }
I know that my device, an MC70, has a camera.
bool hasFeature = packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
int numCameras = android.hardware.Camera.getNumberOfCameras();
Both hasFeature is true and numCameras > 0
The device has an SD card installed:
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
In the AndroidManifest.xml file I have:
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
final PackageManager packageManager = context.getPackageManager();
This list comes back empty, probably a bad sign:
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
This code must run on the MC70, which is limited to KitKat 4 API 19.
But when I call:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
I get the exception:
No Activity found to handle Intent { act=android.media.action.STILL_IMAGE_CAMERA_SECURE (has extras) }
I have tried:
MediaStore.ACTION_IMAGE_CAPTURE
MediaStore.ACTION_IMAGE_CAPTURE_SECURE
MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA
MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE
All throw the exception. Why?
Why?
Because the device manufacturer did not install a camera app that supports any of those Intent structures. If the device does not legitimately ship with the Play Store on it, there is no requirement that the manufacturer have to meet any particular compatibility requirements.
Assuming that you do not need a camera app with any particular features, install an open source one, like Open Camera, so your device has an app that responds to ACTION_IMAGE_CAPTURE, etc.

Android gallery not updated after ACTION_IMAGE_CAPTURE

I'm using MediaStore.ACTION_IMAGE_CAPTURE to take a picture with MediaStore.EXTRA_OUTPUT to point to the Uri for the DCIM/Camera directory where all the other photo/video files are stored.
The photo file is successfully taken and I can see it using ES File Explorer and can view it inside my app. However it is not shown in the gallery when I use Intent.ACTION_PICK
Intent selectPictureIntent = new Intent(Intent.ACTION_PICK);
selectPictureIntent.setType("image/*");
I've read the other topics on updating the Gallery after the picture comes back using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
and also
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
What's going on here :(
why did you use LocalBroadcastManager? LocalBroadcastManager can only send broadcast data inside of your app. Try to use
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
sendBroadcast(intent);
Gallery always listens to URI change. After file event broadcast to Gallery, the data stored in Gallery will be updated.
it also can problem with KITKAT build version.. for sure you can use this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(out); \\out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

How I could call the native viewer on android? Or implements Zoom and rotate the Image?

I have the image's path and I'd like to caLL the native viewer from android, because the user want's the option of zoom and rotate the image. I have to make it by hard or I can call an native Intent?
To call the native viwer :
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/jpg");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(i);
}

How to make a button that functions as a link in eclipse for android

I need to know how I could make a button that opens the androids web browser and navigates to a specific URL. I'm using eclipse and already know how to make buttons and click listeners.
Thanks for any help.
Just add this code to onClick():
// Launch a browser
Uri uri = Uri.parse("http://www.yahoo.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.android.com");
websiteIntent.setData(uri);
startActivity(websiteIntent);
Try this
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com");
startActivity(intent);

Categories

Resources