Android Camera intent creating two files - android

I am making a program that takes a picture and then shows it's thumbnail.
When using the emulator all goes well and the discard button deletes the photo.
But on a real device the camera intent saves the image at the imageUri variable and a second one that is named like if I had just opened up the camera and took a picture by itself.
private static final int CAMERA_PIC_REQUEST = 1337;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
//start camera
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
image = (ImageView) findViewById(R.id.ImageView01);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
//save the image buttons
Button save = (Button) findViewById(R.id.Button01);
Button close = (Button) findViewById(R.id.Button02);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
try{
thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
image.setImageBitmap(thumbnail);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
finish();
}
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.Button01:
finish();
break;
case R.id.Button02:
dicard();
}
}
private void dicard(){
getContentResolver().delete(imageUri, null, null);
finish();
}

Some Android phones store the original photo in the gallery, and a thumbnail only in your location. It doesn't matter what you did with the original request. I have two different HTC phones doing it, and a slew of other brands not doing it.
I solved this another way. I ran a query of every item in the gallery and loaded the BucketIDs to an array. I do this when my app starts the camera app. When the camera app returns, I make the same query (with items recently added to save time). I compare this to my original list and find the new BucketID. Next, I compare the size of this image with the file I explicitly set as the output. If it's bigger, I copy it, replacing what I had. Then I delete the file and remove it from the gallery.
Pain in the you-know-what!
[EDIT] I had to change this around again when I discovered a phone that didn't keep unique bucket IDs... See my post in the link following this answer for more.

Related

How to save multiple selected photos separately to upload to a server in android studio?

I have had a problem for weeks and still can not solve, did a lot of research and tested many codes, but nothing solved. I will explain my problem in detail.
I am making an application where in a layout there would be three or more clickable photos, I am using the “ArthurHub / Android-Image-Cropper” image cropping library, the steps are:
Click on the image, which gives the option to open the image gallery to select a photo or take a new photo, after that I can crop
the selected image.
Select the second photo, do the same as the previous one, thereafter.
Upload the photos to the server.
Steps 1 is working correctly, the problem arises when I try to select from the second image.
What I'm using: I'm using two classes: the one that requests the images and the one that returns:
1. would be the main window with ImageButton.
2. and the other class that returns the selected images to the first class. (Contains the function that calls the gallery or camera, cuts the image, the other “onActivityResult” function that returns the image address, who asked for the picture).
So far everything works correctly.
Problem Description: The problem arises from the second image selected. when the second class returns everything to the first, it is as if the second image is stored in the same memory space as the first (erasing everything previously), and if I select the third image, it deletes the second and only remains the third and henceforth. What I want to do is select the images and have them all seen at the same time (usable) to send to a server.
Solutions I tried:
After days of searching, the suggestion was to make several returns on “onActivityResult” so that it returned the result of selecting
images separately, I couldn't find anything that worked, the
explanations I found were only halfway (including the official
documentation does not detail the steps to control the various returns
of the function, is very superficial), could not control the separate
pointing of the images.
switch (requestCode){
case (1000):
Intent intent1 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent1.putExtra("class", classname);
intent1.putExtra("imageview", imageview);
intent1.putExtra("pathimage", pathimage);
startActivity(intent1);
break;
case (2000):
Intent intent2 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent2.putExtra("class", classname);
intent2.putExtra("imageview", imageview);
intent2.putExtra("pathimage", pathimage);
startActivity(intent2);
case (3000):
Intent intent3 = new Intent(TirarFoto1.this, RegistrarAutomovelDuasRodas.class);
intent3.putExtra("class", classname);
intent3.putExtra("imageview", imageview);
intent3.putExtra("pathimage", pathimage);
startActivity(intent3);
break;
default: break;
}
I decided to save each image separately outside the “temporary cache” folder (where they are located), even with the “manifest”
permissions, nothing happens, permission is denied, so I couldn't even
create the new folder to save the images in it, as a result does not
save the selected images.
if (ContextCompat.checkSelfPermission(TirarFoto1.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
File file = new File(Environment.getExternalStoragePublicDirectory(String.valueOf(imageUri))+"folderName");
if (!file.exists()){
Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
}
if (success){
Toast.makeText(this, "creaty", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Erro!!!", Toast.LENGTH_SHORT).show();
}
}else {
requestStoragePermisson();
}
Result: The first solution did not work and the second did not work.
What I want: a model, a concrete way to help me, can anyone please help me with this? I have been stuck in this problem for a long time.
me to use ArthurHub-Android-Image-Cropper for cropped image, you can learn from this link
https://github.com/ArthurHub/Android-Image-Cropper/wiki
hope this help
Edit:
i have a class using ArthurHub-Android-Image-Cropper but from kotlin and i try to convert to java
like this
//for identify image
private Int imageNo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//......
//set button image 1
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 1
imageNo=1
getImageClick()
}
});
//set button image 2
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 2
imageNo=2
getImageClick()
}
});
//set button image 3
final Button button1 = findViewById(R.id.button_image1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set image count to 3
imageNo=3
getImageClick()
}
});
}
//select image form camera or galery
public void getImageClick() {
CropImage.startPickImageActivity(this);
}
//this for Crope Image
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.start(this);
}
#Override
#SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//handle result from onGetimageClick(button for select image from camera or galery)
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
Uri imageUri = CropImage.getPickImageResultUri(this, data);
//start Crope image
startCropImageActivity(imageUri);
}
// handle result of CropImageActivity
else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
switch (imageNo){
case (1):
//here you have resultUri for save image or preview as image1
break;
case (2):
//here you have resultUri for save image or preview as image1
case (3):
//here you have resultUri for save image or preview as image1
}
}
}
don't forget to add manifest
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Theme.AppCompat"/>
Hope this help

How do I take a picture on Android app, and save photo to gallery in internal storage? (Not SD Card)

I have my app so I have the ability to open the camera and take a picture, the app then shows me a preview of the photo just taken, and when I press 'OK', it is gone. I want to be able to save the image I have taken to my internal storage on my phone, (and possibly have a function so I can access my gallery and preview it later on). I have searched everywhere but all the help I am getting is to store it on an SD card, whereas I want to use my phones built in storage. Thank you
My code so far:
Button butCamera;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
butCamera = (Button) findViewById(R.id.butCamera);
imageView = (ImageView) findViewById(R.id.imageView);
butCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
What you are trying to achieve is quite complex,
an i'm not sure you could use this common storage method to achieve it
File root = Environment.getExternalStorageDirectory();
But have a loot at this post, it should have what you require
android - save image into gallery
To store the Image to Internal storage of the device, you will have to do something like this.
File file = new File(Environment.getExternalStorageDirectory(), "MyFile.jpg");
Uri uri = Uri.fromFile(file);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, CAMERA_REQUEST);

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?

I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?
This works fine.. for selecting and then displaying the image..now what do i do for selecting next and previous images?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I could select an image from gallery in android
Your code happens to bring up a gallery app on your device. You are requesting ACTION_GET_CONTENT for image/*. This means:
Other apps, besides a gallery, can respond to that Intent and can be chosen by the user
There will be devices that do not have an app that would be considered a "gallery"
How do i do it?
Write your own gallery. There is no concept of "next" or "previous" with ACTION_GET_CONTENT.
There are many image picker libraries for Android that might form the basis for your own gallery UI.
I forgot to write the most important thing, which is code for previous and next is not working.. till now i have written code only for previous, coz once it works; next ll be just few changes ahead.
moveToprevious is coming false n cursor is declared as global.. so that when a query for cusor s fired in getcontentresolver() is fired, it gets the needed value..
Please look into this!

Start camera for capturing picture and store image after captureing it in gellary on button click

I am working on android application. I have an Activity in which there is two button first one for selecting image from gallary. i have applied function on it. i have one more button capture image . i want to work on it .but don't know how to start camera .I want that when i click button capture image it should start camera for capture image.and there should be option to cancel if don't want to take picture. after pressing cancel camera should cancel.
if i captures image it should it should show in Image View and automatically store in SD card .how should i proceed.
http://developer.android.com/guide/topics/media/camera.html. Everything you need to know about starting a camera. Go through the link.
private static final int TAKE_PHOTO_CODE = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
get uri
private File getTempFile(Context context){
return new File(path, "/tourpath/yourfilename.jpg");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case TAKE_PHOTO_CODE:
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
iv.setImageBitmap(captureBmp);//show in imageview
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}

How to set and store an image in an Image View or Button in Android?

I have built an android app that lets the user click on an image button. Once clicked the user gets the option to open the Gallery or the camera.
I want that the image (chosen from the gallery or taken with the camera) is displayed in the image button. Furthermore I want that the image or its path is stored to an sql database so that when opening up the app once again the user sees the image he set before.
Can anyone give me a code example of doing just that?
There is no short answer to your question, and by posting it all at the same time will learn you less. Anyway there are lots of tutorials on how to do this out there.
I'm guessing you got the gallery and camera code done. Otherwise you may find it in the links below.
Gallery: Get/pick an image from Android's built-in Gallery app programmatically
Camera: Capture Image from Camera and Display in Activity
When you got your Uri from that captured or chosen picture, you can create a bitmap from it:
public Bitmap getBitmapFromUri(Uri uri)
{
return MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
}
Then you can set the bitmap to the imagebutton using following:
((ImageButton) findViewById(R.id.yourImageButton)).setImageBitmap(bitmap);
One of the links I pasted shows you how to get the path of the image. This path you can either save on your database, or save it with SharedPreferences. You can read about how to use it at the same link. Regarding the database, you know how to get the image path from my answer. Search google for database tutorials. But I think SharedPreferences will be enough depending on the scale of your application.
Good luck!
Click button and set store image in imageview.
`public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 22;
Uri cameraUri;
Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImgPhoto = (ImageView) findViewById(R.id.imageView1);
BtnSelectImage = (Button) findViewById(R.id.button1);
BtnSelectImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImgPhoto.setImageBitmap(photo);
} catch (Exception e) {
Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
} catch (Exception e) {
}
}
}`
Set this Manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

Categories

Resources