Android auto take picture - java

I am working on app that is suppose to take a selfie every 20 minutes without the user interface or user pressing something. I start this job using a JobScheduler and I create a notification that tells the user that my app is working. I've managed to take a slefie but it needs user's action and preview and I do not want this. I just simply want to take a selfie without the preview or user pressing a button.
private void pic()
{
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera,7);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.d("bine","true");
if (requestCode == 7)
{
Log.d("bine","true");
Bitmap photo = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(photo);
}
}
P.S: I am working with android 9!

Related

How to stop reload the data when user open second time Fragment or activity?

"Ex: In flipkart when we open App, Home screen will load the data and when we go to next activity from home screen and again we come back to Home screen its not loading again "
Cancel all the tasks using lifecycle methods of Fragment: "onPause", "onStop" and etc (you have to choose one of them). Fragment lifecycle.
Use onActivityResult
Define constant
public static final int REQUEST_CODE = 1;
Call your custom dialog activity using intent
Intent intent = new Intent(Activity.this,
CustomDialogActivity.class);
startActivityForResult(intent , REQUEST_CODE);
Now use onActivityResult to retrieve the result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//reload data here
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
In custom dialog activity use this code to set result
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
While tap on bottom back button it will not reload, because it just finishes current activity and resumes home activity.
To prevent reload home activity while tap on back button from toolbar, you have to add
android:launchMode="singleTop"
to your home activity in your manifest file.
For more details refer here

How to get photo from stock galleries other than google Photos in Android Lollipop and higher?

I am using following code to pick an image from gallery.
//Override For uploading the image.
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
//for showing the selected image.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RESULT_LOAD_IMAGE && resultCode==RESULT_OK && data!=null)
{
Uri selectedimage=data.getData();
imagetoupload.setImageURI(selectedimage);
}
}
It only picks an image from google Photos application. I want to pick images from "Gallery" application which comes as stock gallery in phones. While I choose from my Gallery application image is not picked and while showing the seleected image no image is shown.
Use ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT, depending on the version of Android you are running on, as is covered in the documentation.

file chooser for android

I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.
I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..
Or is there any other approach I can implement to add a photo...
You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.
private static final int REQUEST_PICKER = 1;
private void invokePicker() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}
Then receive the result on onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
// Get Uri of the file selected,
Uri theImageUri = data.getData();
// Or if you want a Bitmap,
Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
}
}
Edited:
Though in this way you don't need a real file path, you can get it from MediaStore if you need.

Returning Image Resource ID

I'm trying to get a program to let the user to import a custom background.
Here's where I'm at:
I have the getDrawable function taking another function as an argument:
mDrawableBg = getResources().getDrawable(getImage());
getImage() is suppose to return a integer referencing the selected image, here is the code (so far) for that function:
public int getImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
This is suppose to open the gallery and let the user select an image. I would then use mDrawableBg to set the background. I'm not sure how to return a reference ID to that selected image though. Any suggestions?
Try this:
String pathName = "selected Image path";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
The way you're attempting to do it is not possible, I'm afraid. One of the things you'll want to learn as a new Android developer is how the cycle between activities works. In your case, you're running an Activity that calls upon an Intent to get data from it. However, in the Android API, an Intent can only be referenced on its own time. This means you can't use your getImage() method the way you had tried.
There is hope, though!
What you first need to do is call the Intent. You will do this through the code you have now in getImage():
public void getImage() { // This has to be a void!
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
This method will now start the Image Picker that you want users to select from. Next, you have to catch what is returned. This cannot be returned from your getImage() method, but instead must be collected from elsewhere.
You must implement the below method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
final int SELECT_PICTURE = 1; // Hardcoded from API
if (requestCode == SELECT_PICTURE) {
String pathToImage = data.getData().getPath(); // Get path to image, returned by the image picker Intent
mDrawableBg = Drawable.createFromPath(pathToImage); // Get a Drawable from the path
}
}
}
Lastly, instead of calling mDrawableBg = getResources().getDrawable(getImage());, just call getImage();. This will initialize the Image Picker.
Some reading:
Android Activity (notably stuff about Intents and getting a result back)
Android Drawable
Getting a Drawable from a path
More on the Image Picker Intent
Good luck!
I'm not sure, but if you mean you don't know how to receive results from that intent, you can use :
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
if (requestCode == 10)
{
// DoSomething
}
}
}

android camera intent with external camera app, return to original activity?

If a user has an external camera app, such as camera+ that is set as their camera default, how do I make sure that after capturing a photo, it will go back to my original application activity?
public void onClick(View v) {
switch (v.getId()){
case R.id.photo_camera_button:
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoIntent, CAMERA_PHOTO_REQUEST);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == CAMERA_PHOTO_REQUEST){
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
ImageView imv = (ImageView) findViewById(R.id.ReturnedImageView);
imv.setImageBitmap(bmp);
}
}
}
This application is supposed to capture an image and send it back to an imageview, but after capturing a photo, the camera application is still there. I would like it to go back, or would I have to set up from scratch a new camera application?
Although, I would like it to use camera+ features and then when the user saves the image (typically it'll go to my SDcard, I believe) it'll kill the app, and then go back to my activity? Maybe override something?
Any help? Thank you!
What your asking is very hard to answer as i dnt know weather your third party app provide the feature of throwing back the result .You question could be answered properly if you have code of that third party app / see the doc weather they offer there app to be used by some third party
And developing new camera app by your self is not a such big task .

Categories