* EDITED *
public void btnTakePhotoClicked(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
public void btnDeletePhotoClicked(View v) {
iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
iv.setImageDrawable(null);
Toast.makeText(AddIncome.this, "Photo deleted", Toast.LENGTH_SHORT).show();
}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
cameraImage = (Bitmap) data.getExtras().get("data");
imgSpecimenPhoto.setImageBitmap(cameraImage);
}
}
}
Everything works fine except for:
public void btnDeletePhotoClicked(View v) {
iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
iv.setImageDrawable(null);
Toast.makeText(AddIncome.this, "Photo deleted", Toast.LENGTH_SHORT).show();
}
I'm not able to delete photo after pressing the button
Only the Toast "Photo Deleted" is working.
http://i.stack.imgur.com/BhOaU.jpg
Paste this below code in your onCreate() method of activity. Your updated onCreate method should look like this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adddata);
ImageView iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
iv.setImageDrawable(null);
}
Related
i have a button and when click, open camera take photo,the photo save in the sdcard and when i pass it in image view float right i cant understand why, can anybody help please,i want to display the photo with the Height and weight i have in ImageView from layout and only with the camera 2 work with the camera one does work i mean the photo is saved but not display in the image view the code is here
Button takePhoto;
ImageView photo;
static final int Cam_Request = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photo = (ImageView)findViewById(R.id.ivPhoto);
takePhoto = (Button)findViewById(R.id.btnPhoto);
takePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
startActivityForResult(camera_intent,Cam_Request);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String path = "sdcard/camera_app/image.jpg";
photo.setImageDrawable(Drawable.createFromPath(path));
}
private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image = new File(folder,"image.jpg");
return image;
}
}
Try this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo)
}}}
Below is some code which is working somewhat…I currently have 2 ImageViews on my Screen and below each is a button. When the button is clicked the camera function opens. My aim is that the button below one imageviews changes just that image view, leaving me able to then click the other button to change the other. However at the moment when I change one, they both change to the same photo. I had previous tried a few ‘if’ statements by initialising each to a variable called “currentImage” but to no avail.
Any thoughts?
public class NowThen extends Activity {
Bitmap photo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nowthen);
}
public void nowPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
}
public void thenPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView nowImage = (ImageView) findViewById(R.id.nowImage);
ImageView thenImage = (ImageView) findViewById(R.id.thenImage);
nowImage.setImageBitmap(photo);
thenImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
}
Change your thenPhoto code to this:
public void thenPhoto (View view){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,1);
}
And change your onActivityResult to this :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView nowImage = (ImageView) findViewById(R.id.nowImage);
nowImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
} else if (requestCode == 1 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
ImageView thenImage = (ImageView) findViewById(R.id.thenImage);
thenImage.setImageBitmap(photo);
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
}
Your code is correct except that you are starting the intent with same requestCode 0. Due to this the same block is called in your onActivityResult() block.
startActivityForResult(intent,0);//for first button
startActivityForResult(intent,1);//for second button
Now check your requestCode in your onAcitivityResult() code in a if or switch statement.
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 2500;
Button Report_help;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Report_help=(Button)findViewById(R.id.report_help);
Report_help.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.display_image);
imageview.setImageBitmap(image);
}
}
}
This app captures the image and displays in the imageview.But the problem is after I capture the image and press the back button app crashes.I don't know why is this so? Please anyone help.
I think when you press back button
Bitmap image = (Bitmap) data.getExtras().get("data");
in onActivityResult cause the Null pointer exception error, please catch this one.
Use the below code to check that case.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
break;
}
hope this helps you.
how to store the imagePath of the image that I had captured using the camera activity to database. I also need to retrieve that imagePath and display on another activity?
Can someone help me please?
Update:
public class Image_secondPage extends Activity
{
public static final int CAMERA_RESULT = 1;
Button btn1;
ImageView imageView1;
private final String tag = getClass().getName();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image_secondpage);
imageView1 = (ImageView)findViewById(R.id.imageView1);
Button btnNext = (Button)findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Image_secondPage.this, ImagesPage.class);
startActivity(intent);
}
});
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))
{
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);
}
else
{
Toast.makeText(getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.i(tag, "Receive the camera result");
if(resultCode == RESULT_OK && requestCode == CAMERA_RESULT)
{
File out = new File(getFilesDir(), "newImage.jpg");
if(!out.exists())
{
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
imageView1.setImageBitmap(mBitmap);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
imageView1 = null;
}
I am supposed to get the imagePath that I have captured and show the image on the imageView that I had on the next class but I don't know how. Can someone help me?
try use this
bitmap = (Bitmap) data.getExtras().get("data");
this will give you bitmap image, then you can save it to database or anything you want.
let me know if this not solve your problem.
If I have multiple buttons on a view to call camera intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE) and a ImageView for the preview of each image and I need to know which button called it in onActivityResult so I know which corresponding preview to use how do I pass an identifying variable? Below is current code that only works with one image.
Picture button:
final ImageButton cameraTakePhotoButton = (ImageButton) photoPromptOption.findViewById(R.id.cameraTakePhotoButton);
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
final ImageView questionPhotoResult = (ImageView) findViewById(R.id.questionPhotoResult);
questionPhotoResult.setImageBitmap(thumbnail);
}
}
}
Ended up using a ListView and custom adapter then having it iterate over an ArrayList with objects of class Photo... I update the Bitmap (thumbnail) property for the Photo objects when I take the picture then refresh the ListView. This method works very well.
photoListView.setAdapter(new ArrayAdapter<Photo>(this, R.layout.photo_list_item, photosList) {
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View row = null;
final Photo thisPhoto = getItem(position);
if (null == convertView) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.photo_list_item, null);
} else {
row = convertView;
}
photoPreview.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
File photoDirectory = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName");
if(!photoDirectory.isDirectory()) {
photoDirectory.mkdir();
}
File photo = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName/", thisPhoto.getId()+"_photo.jpg");
CameraHandlerSingleton.setPictureUri(Uri.fromFile(photo));
CameraHandlerSingleton.setPhotoId(thisPhoto.getId());
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
return row;
}
});
Then my onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Uri selectedImage = CameraHandlerSingleton.getPictureUri();
String photoId = CameraHandlerSingleton.getPhotoId();
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap thumbnail;
try {
thumbnail = Bitmap.createScaledBitmap(android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage), 300, 200, true);
p.setThumbnail(thumbnail);
p.setTaken(true);
} catch(FileNotFoundException e) {
Toast.makeText(this, "Picture not found.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch(IOException e) {
Toast.makeText(this, "Failed to load.", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
} catch(Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
Log.e("Camera Exception", e.toString());
e.printStackTrace();
}
startActivity(getIntent());
finish();
}
}
}
Yikes. Why not use requestCode? Just tag each view with a unique code, making sure it doesn't clash with any other intents you're throwing around. Alternatively, you can use Object.hashCode() , but again, watch out for clashes.
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST + v.getTag());
}
});
Then check it in your handler:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST + button1.getTag()) {
// do stuff
}
else if (requestCode == CAMERA_PIC_REQUEST + button2.getTag()) {
// do more stuff
}
}
}
If you have many buttons (or items in a ListView) that you're going to handle similarly, you can use a Map to recover the calling view from the tag.