I am trying to capture Image on android and display it on an image view;
the capture intent called when the activity starts ,and image view is in the same activity that calls the capture Intent
when i run the application the camera capture the Image TWICE then display the image in Image view ?! any ideas why ? and how can i fix it?
> Activity.java
public class View extends Activity{
ImageView imgview;
Bitmap Bmp;
final static int cameraData = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
imgview = (ImageView) findViewById(R.id.imgview);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bmp = (Bitmap) extras.get("data");
imgview.setImageBitmap(Bmp); }
}}
> camera.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imgview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Image view"
android:src="#drawable/ic_launcher"
/>
</FrameLayout>
I'm starring at your code and can't see anything, that looks familiar to what I except from a camera application.
Here's the guide to Camera Apps
At a glance, you do the following:
You get a Camera instance and a CameraPreview (a FrameLayout) and put the preview into your layout.
For capturing an image (whatever triggers this in your application) you use Camera.takePicture().
This in turn calls onPictureTaken() of the PictureCallback. Within onPictureTaken() you get the file containing the image data with getOutputMediaFile().
Copy it and have fun with the data.
you better call startActivityForResult() upon an event(say a button click for eg).
Instead of calling directly form onCreate() , add a button and start camera intent inside button click.
Basically just a guess but I think your activities onCreate method is also called after the camera took the first picture(!?) try something like
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
imgview = (ImageView) findViewById(R.id.imgview);
if(!getIntent.hasExtra("data")){
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
}
}
I'm not very sure about this and can't test something right now(no sdks installed over here) but I hope it helps!
Related
I saw a video of how to put an camera access on android studio but I didn´t have much space in my current activity so I made an clicklistener on the image and when the user clicks on image, it goes to another activity where the image appears bigger, but when I click on the image it goes to the other acivity but doesn´t show the image. I saw other questions but or they ask about how to pass an image that is on drawable resources or the answer doesn´t fit me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fazer_perguntas);
photoCamera = (RelativeLayout) findViewById(R.id.photoCamera);
photo = (ImageView) findViewById(R.id.photo);
photo2 = (ImageView) findViewById(R.id.photo2);
photo3 = (ImageView) findViewById(R.id.photo3);
photoCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera,0);
}
});
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent p = new Intent(getApplicationContext(),BigPhoto.class);
p.putExtra("idphoto",photo.getId());
p.putExtra("idphoto2",photo2.getId());
p.putExtra("idphoto3",photo3.getId());
startActivity(p);
}
});
{
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
photo.setImageBitmap(bitmap);
Intent selfiSrc = new Intent(this, BigPhoto.class);
selfiSrc.putExtra("data",bitmap);
startActivity(selfiSrc);
}
}
}
This is part of my firstactivity java class. photo2 and photo3 are other imageviews that I will do the same thing for photo.
package com.example.ritalopes.help;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class BigPhoto extends AppCompatActivity {
ImageView photo, photo2, photo3, bigphoto;
private Uri path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_big_photo);
photo = (ImageView) findViewById(R.id.photo);
photo2 = (ImageView) findViewById(R.id.photo2);
photo3 = (ImageView) findViewById(R.id.photo3);
bigphoto = (ImageView) findViewById(R.id.bigphoto);
Intent pho = getIntent();
int image1 = pho.getIntExtra("idphoto",0);
int image2 = pho.getIntExtra("idphoto2",0);
int image3 = pho.getIntExtra("idphoto3",0);
Intent take = getIntent();
Bitmap bitmap = (Bitmap) take.getParcelableExtra("data");
bigphoto.setImageBitmap(bitmap);
}}
This is my secondactivity javaclass where I want to pass the image(s) to. I tried that code envolving a Uri but didn´t worked out.
Thanks in advance for your responses.
Your code is not very clear...
First, you have to add an OnClickListener for all images, not only for photo
Then, you have to pass extra data to Intent, like you've done. But, don't pass the ID of the imageView but the drawable ID you want to show in fullscreen.
Do you want to display images for drawable ? Or Bitmaps ? If bitmap, I think intent.putExtra("data", bitmap) will work as Bitmap is Parcelable.
if your purpose is sending an image in order to preview image in the another activity. I recommend you try to use this library. just add it in your gradle file in your project.
https://github.com/chathuralakmal/AndroidImagePopup
this preview image library have using Picasso Library to you can show your preview is so easy. and it also contain close button for you.
it help me , i hope this would save you time.
I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
I have a method that needs to be called from another activity, and I need to use it to set an ImageView in it's own activity. I have this method in MainActivity:
public static void setImageView(String fileName){
Log.i(TAG, fileName);
imageView = (ImageView) findViewById(R.id.imageView);
bmp = BitmapFactory.decodeFile(fileName);
imageView.setBackgroundResource(0);
imageView.setImageBitmap(bmp);
}
But I can't make a static reference to findViewById because it isn't a static method. This method is being called in a Camera Activity after the photo has been saved, I want to pass in the fileName (file URI) and set the imageView such that when the Camera Activity finishes and the user return to the MainActivity the ImageView is already set. As such, in CameraView I am trying to call this:
...code...
mCamera.takePicture(null, null, callback);
MainActivity.setImageView(fileName);
Is there a cheeky way around this? I know there are other posts on this but I can't quite work out how to apply the advice given there to my situation.
Thanks!
Maybe you should launch your CameraActivity using startActivityForResult() method, and after take the photo put the fileName as an Extra into an Intent and set it as result. Then in your MainActivity you can get the fileName back from the Intent arg of onActivityResult().
Something like:
public class MainActivity extends Activity{
public void aMethod(){
...
Intent i = new Intent(this, CameraActivity.class);
startActivityForResult(i, REQUEST_CODE);
...
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Bundle b = data.getExtras();
String fileName = b.getString(RETURN_FILE_PARAMETER);
doSomething(fileName);
}
}
}
...
public class CameraActivity extends Activity{
private void returnFileFinishActivity(String fileName) {
Intent retIntent = new Intent();
retIntent.putExtra(RETURN_FILE_PARAMETER, fileName);
setResult(RESULT_OK, retIntent);
finish();
}
}
Regards.
A few things.
1) Since you should only be displaying one activity at a time, why not just start with startActivityForResult in the activity with the ImageView and override onActivityResult in your camera activity?
2) I'm not sure of your application, but it may be easier if you implement taking a picture by following this: http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/ .
3) You can expose a static reference to your main activity, in your main activity's onCreate method, do something like staticRef = this; and in your camera activity simply access it via MainActivity.staticRef... (I would not recommend this approach)
4) You can register a broadcast receiver in your Main activity that has a reference to your main activity or image view and in your camera activity you send a broadcast to it which you can set the image view
I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device. I want to send image from one class to another so it can appear in ImageView. I am searching from many hour but i got only about sending text data from one class to another.Can any one tell about sending an image from one class to another?
This is code from sender class which will take image.
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
bmp=(Bitmap)extras.get("data");
}
}
For any help thanks
You get Image in your Activity as a Bitmap and you also pass that to another Activity as Bitmap with Intent.putExtra() like this:
First Activity.
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp);
and get from second Activity like:
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
you don't need to get url and load from url.
that is the simplest way to pass the captured image from one Activity to another Activity.
I remember something about that there is a limitation in size for putExtra() and getExtra() about 1mb. So a picture may exceed this limitation.
How about just passing the path to the picture?
My preferred way (and I think the most straight-forward way) is to use an own Application instance in the app, to store variables that are common to more than 1 activity.
Create a class, let's call it MainApplication that extends android.app.Application
and declare it in the manifest:
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
Then you get an instance of the application object in the Activity like this:
MainApplication application = ((MainApplication)getApplication());
Inside this application object you can store any app-level data and use it as usual:
application.setImage(...);
application.getImage();
I got the answer you need to send path of image from one activity to another.
filePath is the path of image.
Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);
And get the path in another activity
final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);
Take One Global.class and Declare public static Bitmap bmp;
takeImg.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_UP)
{
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras();
Global.bmp=(Bitmap)extras.get("data");
}
}
And When u want to use Bitmap bitmap = Global.bmp ;
I'm going to show you the best way okay.
1st) Get and send the image URI
Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);
2nd) Receive the image and how to show it
receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);
I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.
public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();
}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}
After you recover the bmp in your nextActivity with the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
}
I hope my answer was somehow helpfull. Greetings
You could use a Singleton Object to store your Image:
public class SingletonModel {
private Bitmap Image;
private SingletonModel;
public static SingletonModel getInstance() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
public Bitmap getImage() {
return this.Image
}
public Bitmap setImage(Bitmap ImageIn) {
this.Image = ImageIn;
}
}
And in your first Activity put:
SingletonModel.getInstance().setImage(image);
And in your second Activity:
Bitmap image = SingletonModel.getInstance().getImage();
In alternative you could create an Object which extend Application, so this Object is visible for all class (the idea is the same to a Singleton Object).
I'm trying to take apicture and I want to pass this picture to another activity to be displayed in it.
OnClickListener listener_TakePic = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
Bitmap image = (Bitmap) data.getExtras().get("data");
}// End IF
}// End of Method
Please tell me what should I do or modify to achieve what I want..
Actually, i have created an application class "base class" in which, I have defined an "bitmap cameraPic" and "static final int CAMERA_PIC_REQUEST = 1;"
Now, I have an activity classed named "AddLocation" from which I call startActivityForResult(intent, CAMERA_PIC_REQUEST);
but I want the result of the camera Picture to appear in another activity called "MPActivity"
My question is, should I call "onActivityResult(int requestCode, int resultCode, Intent data)" method from "MPActivity" or there is no need to call it...
i'm really confused please clarify..
There are more than one way to tackle the task you are doing.
you can build a java class and put populates its one of property by the desired image. Then you can just get stuff that into an arraylist and pass that to a global arraylist.
you can use Application class and make a global image variable that can be accessed in every class in your activity.
etc ...