Retain selected image from gallery to a new activity in android - android

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();

Related

Android get image from gallery into two imageviews

i searched and i found how to can to get image from gallery and show it in image view.this is a my source
public class CredoUploadFile extends Activity {
private EditText credouploadfileuserid, credouploadfileusername,
credouploadfileusersurname;
private int RESULT_LOAD_IMAGE = 1;
private int choosebutton;
private RoundedImageView credouploadcenter1left,credouploadcenter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_credo_upload_file);
credouploadcenter1 = (RoundedImageView) findViewById(R.id.credouploadcenter1);
credouploadcenter1left = (RoundedImageView) findViewById(R.id.credouploadcenter1left);
credouploadcenter1left.setOnClickListener(new 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);
choosebutton=1;
}
});
credouploadcenter1.setOnClickListener(new 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);
choosebutton=2;
}
});
}
#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();
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
}
}
}
i can to show image from gallery in image view,but i have two imageviews and when i click second imageview and choose some image result is first image view.i can't to show image only imageview witch i clicked.
how i can solve my problem? if anyone knows solution please help me
In onActivityResult you use the same variable credouploadcenter1left for both if...
Try:
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory .decodeFile(picturePath));
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Here is the error, independently of your choose, you will set image always on credouploadcenter1left.
You have to set it on centeruploadcenter1 when you choose second button.

Attach imageView to email

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!

how to select image in one class and display it on image view on other class

i am using a button to select an image from gallery and the image will be set to imageview which is on other screen:
//setimg button is on firstscreen.xml
setimg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
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();
// String picturePath contains the path of selected Image
ImageView iv_wallset = (ImageView) findViewById(R.id.iv_wallset);
iv_wallset.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
// Imageview iv_wallset is on second.xml
can we use intent.putextra() to carry image from one screen to other???
Yes, you can put an extra String in the intent and from the second activity get the argument and decode the picture:
in activity 1:
mIntent.putExtra("image_path", picturePath);
in activity 2:
String path = getIntent().getStringExtra("image_path");
ImageView imageView = (ImageView) findViewById(R.id.iv_wallset);
imageView.setImageBitmap(BitmapFactory.decodeFile(path));
yes convert image into byte array and pass that byte array to other activity.

Android onActivityResult Query

I have two Activities A,B
From Activity A, I do open my gallery and I want that when the picture is selected from the gallery it should go on Activity B and not on Activity C.
Is this possible??
share_picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent choosePic = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePic, LOAD_IMAGE_GALLERY);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LOAD_IMAGE_GALLERY && 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]);
picturePath = cursor.getString(columnIndex);
//I WANT TO CALL ACTIVITY B FROM HERE.. THAT AFTER THE PICTURE IS SELECTED IT SHOULD GO ON ACITIVITY B AND NOT ON A.
}
}
Thanks
Put in intent extra your filePathColumn.
Finish Activity C;
And call Activity B with intent;
just write this code inonActivityResult after picturePath = cursor.getString(columnIndex);
// used to show HD images
BitmapFactory.Options bounds = new BitmapFactory.Options();
// divide bitmap to 4 sample size it can be 2rest(2,4,8 etc)
bounds.inSampleSize = 4;
// get bitmap from bounds and file path
Bitmap bmp = BitmapFactory.decodeFile(filePath, bounds);
imageView1.setImageBitmap(bmp);
Now here write Intent code
Intent intent= new Intent(A.java,B.class);
startActivity(intent);
You just need to pass the intent from your onActivityResult() inside ActivtyA to ActivityB passing picturePath through intent
ActivityA.java
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Insert it once you got the picturePath through Content Resolver
picturePath = cursor.getString(columnIndex);
Intent forwardToB=new Intent(getApplicationContext(),ActivityB.class);
forwardToB.putExtras("PATH",picturePath);
startActivity(forwardToB);
}
ActivityB.java
Intent i=getIntent();
String pathToImage=i.getStringExtra("PATH");
OR
Bundle extras = this.getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("PATH");
}
Now you can do whatever once pathToImage is inside your ActivityB

Android - Image does not display from built-in gallery

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);
}
}
}

Categories

Resources