Take picture without saving it - java

I want to call the android's native camera and don't save it in the storage, because I save it myself after processing image. It works almost for all devices, except Motorola XT1032 (Android 5.1, API 22). So is there a way to tell the camera not to save the picture?
Here is how I call the camera intent.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "image_"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
((Activity) mContext).startActivityForResult(intent, 1001);

Create the request code variable:
private static final int REQUEST_CAMERA = 1;
Open the camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
and in case you want to show it to the user:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CAMERA:
if (resultCode == RESULT_OK) {
// successfully captured the image
Bitmap mBitmap = (Bitmap) data.getExtras().get("data");
if (mBitmap != null) {
imageView.setImageBitmap(mBitmap);
}
}
}

Related

Update user profileimage in firebase Android studio

I have an app where a user is registered and can have a profile image. In the realtime database I have these information saved. Now I want to be able to change the informatiom, particulary profile image. Here is my realtime database:
When the user clicks on his/hers image the camera intent starts:
private void startCamera() {
//start the camera
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt,REQUEST_IMAGE_CAPTURE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
//picture as image
Bitmap bp = (Bitmap) data.getExtras().get("data");
mProfileRoundedImageView.setImageBitmap(bp);
//here I want to save Image uri and update my firebase with the new uri...
}
}
}
How can I do that?? I have searched alot how to convert captured image to uri and then update imageurl but with no luck. Please help.
BTW I do not want to save the image to the sd, just convert to uri and update firebase...
You can use the below function.
public Uri getImageUri(Context inContext, Bitmap inImage) {
Bitmap OutImage = Bitmap.createScaledBitmap(inImage, 1000, 1000,true);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), OutImage, "Title", null);
return Uri.parse(path);
}
Just call it in the onActivityResult() after you extract bitmap
Feel free to ask if something is unclear

Upload your own profile picture

When I start the application, I upload an image using Picasso to imageview. I'm trying to make the user upload their own image. If I take a picture or select it from the gallery, I can display it in an imageview. But I would like to use SharedPreferences to display the image the next time I load the application, which I unfortunately can't do.
Here is my code.
private void showImageOptionDialog(){
final String[] options = getResources().getStringArray(R.array.image_options);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.alert_dialog_title)
.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent cameraIntent = new Intent();
cameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 11);
break;
case 1:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 9);
break;
case 2:
SaveSharedPreference.setAvatar(context, "none");
recreate();
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Check if the intent was to pick image, was successful and an image was picked
if(requestCode == 9 && resultCode == RESULT_OK && data != null){
//Get selected image uri from phone gallery
Uri selectedImage = data.getData();
//Display selected photo in image view
//head_image.setImageURI(selectedImage);
assert selectedImage != null;
SaveSharedPreference.setAvatar(context, selectedImage.toString());
}
//Handle camera request
else if(requestCode == 11 && resultCode == RESULT_OK && data != null){
//We need a bitmap variable to store the photo
Bitmap bitmap = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data");
//Display taken picture in image view
head_image.setImageBitmap(bitmap);
}
recreate();
}
Then I try to load using
Picasso.get()
.load(SaveSharedPreference.getAvatar(context))
.placeholder(R.mipmap.ic_launcher)
.transform(new CropCircleTransformation())
.into(head_image);
Thank you for your help
(application runs on Android 7.0+)
It's NOT a good idea to save a photo using SharedPreferences. Try caching it or use a local Database or File system to store and/or restore them

how to pick a file in SD card?

I'm trying to make an app with Android Studio that can select a file in SD card and get its path, like an OpenFileDialog, I've tried this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
However, it does not work, how can I do it ?
Try this:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("*/*"); //set mime type as per requirement
startActivityForResult(mediaIntent,0);
You can change type according to your need.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0
&& resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}

How can i make sure the image is not saved to gallery?

I'm making an app that will let users password-protect photos. So far, the user can take a photo and the photo gets displayed. However, all the photos get saved to camera roll. How can I save them to the app instead of camera roll so they can be private, but still be able to access them from their uri (which I will save to SharedPreferences) ?
int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
Uri imageUri;
public void takePic(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "filename_" +
String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra("data", imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Bundle extras = data.getExtras();
Log.e("URI", imageUri.toString());
Bitmap bmp = (Bitmap) extras.get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bmp);
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
}
create a file named (.nomedia) in the folder, then all the photos in this folder will not be shown!
OR
Save your photos on Internal storage.

Building a Camera App - Receive

I am new to Android programming and I'm writing an application in Java that opens the camera take a photo and save it. I made it via Intents but I can't see onActivityResult running.
I have tested it into my phone (Samsung Galaxy S) and when I take the photo I receive a preview of that photo having two buttons one Save and the other Cancel. I haven't added something to my code to do this so I think it's something that camera does. I want after capturing the image to run onActivityResult (after I press the Save button on the preview).
But how I'm going to return a result to start onActivityResult after pressing the Button Save on the preview?
I FORGOT to tell that after i press save my entire app is terminated.
Here is my Code
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TakePicButton = (Button) findViewById(R.id.TakePicture);
TakePicButton.setOnClickListener((android.view.View.OnClickListener) this);
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.TakePicture){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
try the below code, you will have to modify it a bit, it will help you get From Library and From Camera both, the SELECT_PICTURE is used for getting image from library
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null)
myFile = new File(selectedImagePath);
else if (filemanagerstring != null)
myFile = new File(filemanagerstring);
if (myFile != null) {
Bitmap bmp_fromGallery = decodeImageFile(selectedImagePath);
break;
case CAMERA_REQUEST:
Bitmap bmp_Camera = (Bitmap) data.getExtras().get("data");
break;
default:
break;
}
}

Categories

Resources