Android : Gallery - android

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

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.

Problems getting Image past AlertDialog

I have some code that get's a pic from the camera gallery. It works fine when you use a button from an activity to get pic and put pic on that activity but I cant get it to my Imageview when I try this with a button on AlertDialog. Any help?
This code by itself is fine
((Button)dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
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);
System.out.println("Image Path : " + selectedImagePath);
im1.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);}});
But when you add this code to AlertDialog it wont function anymore
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(dialogView);
final AlertDialog alertDialog = alertDialogBuilder.create();
((Button) dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
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);
System.out.println("Image Path : " + selectedImagePath);
im1.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);
}
});
alertDialog.show();
return true;
}
});
I have tested the code on another activity from a Button and it works fine but when I open alertDialog to do it, I cant get the pic back to my imageview. I can still get to my Gallery and click on a pic but when I try to save it it wont put the pic in the imageView.
Try moving the method onActivityResult outside the click listener alongside your other activity methods, like so
public class MyActivity extends Activity {
// other methods and fields
private ImageView im1;
// initialize im1 in your onCreate() method
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}
}
}
}

Android photo browse upload Error : Failure Delivering Result resultinfo{who=null......}

Whenever I try to click on the browse button , I want to browse my gallery and select a photo from it and display it in a new activity.
Until I displayed the photo on the same activity there was no error , but as soon I change the code to transfer the photo on another intent there is a force close error
Here is my code:
Main screen :
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent1 = new Intent(MainActivity.this,Edit.class);
startActivity(intent1);
}
});
Another screen :
public class Edit extends Activity
{
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
private ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
img = (ImageView)findViewById(R.id.ImageView1);
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);
System.out.println("Image Path : " + selectedImagePath);
img.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);
}

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

How do I change the image in ImageView more than once?

My application adds a photo from your gallery to an ImageView just fine, but if I try and change the image (after one has already been selected) my app force closes. Is there a way to achieve this?
here is my code..
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.myimageview);
((Button) findViewById(R.id.taptoadd))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
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);
System.out.println("Image Path : " + selectedImagePath);
img.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);
}
}
Set the image through code .
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

Categories

Resources