I am facing a problem in selecting the image from a gallery and setting it into the imageview. Suppose I have two activities; mainActivity containing buttons for gallery and secondactivity containing the imageview in which the image has to be displayed.
b1.setOnClickListener(new OnClickListener() {
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);
}
});
Please give me the individual code for both....
here is the code to load an image from gallery:
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
with Picasso it can be done in single line and You dont need to make cursor query
I have extended it for better understanding :-
Pick Image
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
Load Image
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();
Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
.into((ImageView) findViewById(R.id.imageView1));
}
}
}
In my case work this solution
private void OpenGallery(){
Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
getImageIntent .setType("image/*");
startActivityForResult(getImageIntent , IMAGE_PICKER );
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== IMAGE_PICKER && resultCode == RESULT_OK) {
Uri fullPhotoUri = data.getData();
imageView.setImageURI(fullPhotoUri);
}
}
You can share URI using putExtra("fullPhotoUri", fullPhotoUri.toString()) between activities.
Try this.
b1.setOnClickListener(new View.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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Here some examples
http://viralpatel.net/blogs/pick-image-from-galary-android-app/
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
To Pick Image from gallery, use this code.
btn_imageSetter.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"), IMAGE_PICKER);
}
});
Now use this Method to Set the image to the ImageViewer
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
try{
final Uri uriImage = data.getData();
final InputStream inputStream = getContentResolver().openInputStream(uriImage);
final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
iv_image.setImageBitmap(imageMap);
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
}
}
}
Related
This question already has answers here:
onActivityResult is not being called in Fragment
(41 answers)
Closed 7 years ago.
How to get image from gallary to set in Imageview in fragment ? onActivityResult not called in fragment..this the code i wrote in fragment
img_user.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);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
img_user.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close`enter code here`();
} else {
Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT)
.show();
}
}
You can do this in this way....in your main activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//call super
super.onActivityResult(requestCode, resultCode, data);
}
and inside your fragment
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// ******** code for crop image
i.putExtra("crop", "true");
i.putExtra("aspectX", 100);
i.putExtra("aspectY", 100);
i.putExtra("outputX", 256);
i.putExtra("outputY", 356);
try {
i.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(i, "Select Picture"), 0);
}catch (ActivityNotFoundException ex){
ex.printStackTrace();
}
and
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode == Activity.RESULT_OK){
try {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
img_user.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Works perfect for me
I was also facing the same problem. Actually callback for startActivityForResult() is coming back to parent activity so you need to make
explicit call from fragment to onActivityResult() function as follows.
In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.
In Parent Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getFragmentManager().findFragmentById(R.id.container);
fragment.onActivityResult(requestCode, resultCode, data);
}
In Child Class:
That Extends fragment perform your logic for image setting on imageview.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cursor.close();
} else {
Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
Please find the following code, it will work. Add this in your fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//Your stuff
}
someone can tell me what the problem, it is not working, so please help fast i really need:
imagePick.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 Contact Image"),1);
}
});
public void onActivityResult(int reqCode, int resCode, Intent data)
{
if(resCode==RESULT_OK)
{
if(reqCode==1) {
imageURI=data.getData();
iv.setImageURI(data.getData());
}
}
}
This is working for me.
private final static int SELECT_PHOTO = 12345;
imagePick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
imageView.setImageBitmap(bitmap);
// Do something with the bitmap
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}
}
Is there a way that I can change ImageView of Main.class class through Gallery.class?
my Main class onClick calls the method setImage of Gallery.class
public void setImage(int currentView) {
Log.d("fgdsf","dumaan dito");
this.currentView = currentView;
Intent gallery = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, RESULT_LOAD_IMAGE);
}
and this is the onActivityResult
#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();
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_main, null);
if (currentView == 0) {
ImageView imageView = (ImageView) v
.findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if(currentView==1)
{
ImageView imageView = (ImageView) v
.findViewById(R.id.imageView2);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
and I get this error
08-07 08:35:08.208: E/AndroidRuntime(15689): Caused by: java.lang.NullPointerException
08-07 08:35:08.208: E/AndroidRuntime(15689): at android.app.Activity.startActivityForResult(Activity.java:3370)
I don't think you can. You can only control the layout of your current activity, along with its components. Since the component isn't on the view of your current Activity (in your case is Gallery), accessing it will throw the NullPointerException. What you can do is, pass the path of the selected image on you Gallery to the Main class then retrieve the image path and set it to your ImageView.
I think your best option is to set an interface in Gallery.class, like
public class Gallery extends Activity {
public interface MyClickListener {
public void OnClick();
}
private MyClickListener listener;
Then in your onClick, set your OnClick ( listener.OnClick() ). In Main.class you will have to import 'MyClickListener' and define whatever actions you want to perform.
For capture Image
This is happens from android 4.0 onward
Before it was returning the image uri path, but now it returns Bitmap from gallery.
So to resolve this.
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
Uri imgUri = Uri.fromFile(file);
String imagepath = file.getAbsolutePath();
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagepath);
startActivityForResult(intent, CAPTURE_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == CAPTURE_IMAGE) {
imgUser.setImageBitmap(BitmapFactory.decodeFile(imagepath);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
For Pick Image From Gallary
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
imgUser.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData()));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
I'm trying to get the complete path of an image file as a String, but it doesn't work. I'm just getting a result like "content:/external/media/images/1". That's definitely not the correct path. How can i get the correct path including the file extensions?
Here is what i tried so far:
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnGetImage:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select A Picture"),
PHOTO_GALLERY);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
File file = new File(data.getDataString());
String imagePath = file.getAbsolutePath();
break;
}
}
}
try this code:
case R.id.btnGetImage:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
break;
and
#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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
picturePath is your required path ...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
File file = new File(selectedImagePath );
break;
}
}
}
You have define correct filename to your path which must be exist in your sdcard, while fetch from sdcard,
String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+test.png;
I smell you trying to do something like this.
I have an activity, which has a button. When I click on the button it redirects me to the image gallery. I want to show the selected image in the next activity using an image view. But it is not displaying the image. The view is off screen when the image is set.
My code for selecting image and moving on next is given below. I am using no history true in my activities.
#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 (!(picturePath.equals(""))) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ImageInGellary.class);
intent.putExtra("picturePath", picturePath);
startActivity(intent);
}
}
}
public class ImageInGellary extends Activity {
Button cancel;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.load_image);
cancel = (Button) findViewById(R.id.buttonCancelPicture);
Intent in = getIntent();
savedInstanceState = in.getExtras();
String picturePath = savedInstanceState.getString("picturePath");
ImageView imageView = (ImageView) findViewById(R.id.img_view);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
cancel.setOnClickListener(new OnClickListener() {
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);
*/
Intent intent = new Intent();
intent.setClass(ImageInGellary.this, MainActivity.class);
startActivity(intent);
}
});
}
}
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
To support android 11 you need to add this code in AndroidMainfest.xml
<queries>
<intent>
<action android:name="android.intent.action.GET_CONTENT" />
<data android:mimeType="image/*"/>
</intent>
</queries>
If you want to achieve it in 1 line then all you need to do is :
Picasso.with(MainActivity.this).load(data.getData()).noPlaceholder().centerCrop().fit().into((ImageView) findViewById(R.id.imageView1));
Complete Solution :-
Pick Image
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
Load Image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
//Get ImageURi and load with help of picasso
//Uri selectedImageURI = data.getData();
Picasso.with(MainActivity1.this).load(data.getData()).noPlaceholder().centerCrop().fit()
.into((ImageView) findViewById(R.id.imageView1));
}
}
}
In your situation you need to pass ImageURI to next activity
Uri selectedImageURI = data.getData();
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
gallery.setType("image/*");
startActivityForResult(gallery, 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){
Uri imageUri = data.getData();
imgView.setImageURI(imageUri);
}
}
I have solved your problem. After the image is picked, convert the image Uri to String and on your second class, convert your string back to image.
http://whats-online.info/science-and-tutorials/95/Android-get-image-from-gallery-into-imageview-programmatically/
http://whats-online.info/science-and-tutorials/49/Android-passing-multiple-data-from-one-activity-to-another/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
if(selectedImage !=null){
Intent intent=new Intent(MainActivity.this, Activity2.class);
Bundle extras = new Bundle();
extras.putString("image", selectedImage.toString());
intent.putExtras(extras);
startActivity(intent);
}
}
}
Activity2.class
Bundle extras = getIntent().getExtras();
String name = extras.getString("image");
Uri imageFinal=Uri.parse(name);