I have an activity A. Starting an activity B from A. In activity B, I capture an image with camera present in the device and at the end of that activity come back to Activity A. In this activity have to display the captured image. How to accomplish this task? Running on version 2.3.3...Have had a look here Capture Image from Camera and Display in Activity but the same NullPointerException...Running on LG device.
You can pass the URL of the captured image from Activity B to Activity A using intent.putExtras methos.
Refer Passing string array between android activities
For Capturing image refer below code
public class Camera extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private String selectedImagePath;
WebView webview;
String fileName = "capturedImage.jpg";
private static Uri mCapturedImageURI;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview=(WebView)findViewById(R.id.webView1);
}
public void TakePhoto()
{
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == CAMERA_REQUEST)
{
selectedImagePath = getPath(mCapturedImageURI);
//Save the path to pass between activities
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Related
I want to attach imageView to send through Android mail services. This is how i get image from Gallery in Activity A:
public class Activity A extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_gallery);
private static int RESULT_LOAD_IMAGE = 1;
Button viewcards=(Button)findViewById(R.id.viewcards);
viewcards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn =
{ MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Intent newdata = new Intent(SavedCards.this, Cardd.class);
newdata.putExtra("picture_path", picturePath);
startActivity(newdata);
}
}}
Transfer image to another class and and put imageView in Activity B:
public class Activity B extends Activity {
private ImageView cardimage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carddd);
String temp = getIntent().getStringExtra("picture_path");
cardimage = (ImageView) findViewById(R.id.cardimage);
cardimage.setImageBitmap(BitmapFactory.decodeFile(temp));
and in Activity B, i want to attach this image into e-mail by onClick.
txtsend=(TextView) findViewById(R.id.txtsend);
txtsend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
As a reference, how image can attach e-mail is here, but this is to pick from gallery.
How to do this by taken image which is already set on imageView. Thank you.
In your code you setup image to ImageView from file in Activity B. And you know path to this picture file. When you send email just attach this file.
Uri uri = Uri.parse("file://" + attachmentFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
You can use ImageView's getDrawable method see this http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()
So something like,
Bitmap imgViewBmp = ((BitmapDrawable)cardImage.getDrawable()).getBitmap();
Should work.
PS: I would suggest you to first check the api reference docs for any class you want to use. Chances of finding what you are looking for is quite high. Next best thing would be to actually google what you want to solve, if that doesn't work then "rephrase" you words and try again. The chances that you will find your solution are doubled (if not more) with the latter!
i want to select an image from gallery and display the same in next activity. On implementing , no image gets retained .
Here is my code :
Activity 1 :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bot=(Button)this.findViewById(R.id.buk1);
bot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
//startActivity(intent);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
cursor.close();
Intent i = new Intent (MainAct.this,gal.class);
i.putExtra("data",selectedphoto);
startActivity(i);
}
}
Activity 2:
public class gal extends Activity{
ImageView view = (ImageView) findViewById(R.id.imger);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galr);
Bitmap selectedphoto =(Bitmap)this.getIntent().getParcelableExtra("data");
view.setImageBitmap(selectedphoto);
}
}
Get the selected images.
Put them in an ArrayList<Bitmap>.
Put it in extras of your Intent.
Retrieve in next activity.
Passing bitmap through intent can be very expansive so pass the path and decode again
Try it this way -
Intent i = new Intent (MainAct.this,gal.class);
i.putExtra("data",filePath);
startActivity(i);
in gal activity get the path and decode bitmap again.
Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity.
intent.setData( uri );
In your ShowImage activity do:
URI imageUri = getIntent().getData();
I am working on app for editing photos.
I have a button in first activity and ImageView in second activity. When I click the button it would open gallery and I would be able to select an image. The selected image needs to appear in my ImageView in second activity but it doesn't. For time being i am displaying image in first activity it self but can anyone suggest me how to display that image in next activity.
Below is my code.
public class Camera_Gallery_Option extends Activity {
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_gallery_option);
Button galleryButton= (Button) findViewById(R.id.button1);
galleryButton.setOnClickListener(new View.OnClickListener() {
#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"),
SELECT_PICTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageview.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Pass the selectedImageUri to the second activity and set the uri to image view in the second activity ?
startActivity(new Intent(this, SecondActivity.class).setData(selectedImageUri));
In the SecondActivity:
protected void onCreate(Bundle savedInstanceState) {
...
imageView.setImageURI(getIntent().getData());
}
I am trying to use Android's built-in gallery. I am able to get the gallery and the albums, but whenever I want to display the image, the gallery straightaway directs me back to my app. I am unable to view the image despite it has been called.
This is my code:
public class CameraTab extends Activity implements OnClickListener{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_tab);
ImageButton cameraBtn = (ImageButton)findViewById(R.id.camera_btn);
cameraBtn.setOnClickListener(this);
ImageButton galleryBtn = (ImageButton)findViewById(R.id.gallery_btn);
galleryBtn.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == this.findViewById(R.id.camera_btn)){
/// some codes here
}
if (v == this.findViewById(R.id.gallery_btn)){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Can anyone please help me? Any help would be appreciated!! Thanks!!
The problem is a misunderstanding of Intent.ACTION_GET_CONTENT intent. It's intented to be used to choose a content (in this case image/*) from the archive.
If you want to show an image, just create a new activity with an ImageView in its layout. Pass the image URI using setData.
I think the Action you want to use is Intent.ACTION_VIEW.
try this
final Intent intent = new Intent(Intent.ACTION_VIEW);
final Uri uri = <The URI to your file>;
// Uri either from file eg.
final Uri uri = Uri.fromFile(yourImageFileOnSDCard);
// or from media store like your method getPath() does but with the URI
// from http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI
intent.setDataAndType(uri, "image/*");
startActivity(intent);
The string "image/* is the mime type to be used.
Now the gallery is opened with the given picture selected. To return to your app the user has to press the back button, as usual ;)
private Context context;
public void onCreate(Bundle savedInstanceState) {
...
context = this;
}
I used a scaled Bitmap below because on some devices the images in the Gallery might be too large for being displayed on an ImageView (I had this problem before).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Bitmap b = new BitmapDrawable(context.getResources(),
selectedImagePath).getBitmap();
int i = (int) (b.getHeight() * (512.0 / b.getWidth()));
bitmap = Bitmap.createScaledBitmap(b, 512, i, true);
// To display the image, you need to set it to an ImageView here
ImageView img = (ImageView) findViewById(R.id.myImageView);
img.setImageBitmap(bitmap);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_choice_activity);
Button takePictureButton = (Button)findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, IMAGE_CAPTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == IMAGE_CAPTURE){
if(resultCode == Activity.RESULT_OK){
// I should get the path here
}else if(resultCode == Activity.RESULT_CANCELED){
}
}
}
In the above code I am trying to get the path of the captured image. I should not use EXTRA_OUTPUT to specify a custom path to save the image. How can I do that?
Use this in ur onActivity result
final Uri contentUri = data.getData();
final String[] proj = { MediaStore.Images.Media.DATA};
final Cursor cursor = managedQuery(contentUri, proj, null, null, null);
final int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
final String path = cursor.getString(column_index);
Kindly check, http://developer.android.com/guide/topics/media/camera.html#intents
MediaStore.EXTRA_OUTPUT : This setting requires a Uri object specifying a path and file name where you'd like to save the picture. This setting is optional but strongly recommended. If you do not specify this value, the camera application saves the requested picture in the default location with a default name, specified in the returned intent's Intent.getData() field.
so, check the Intent data in onActivityResult()