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);
Related
This question already has answers here:
android pick images from gallery
(19 answers)
Closed 5 years ago.
Source Code for get image from gallery and set it to image view and i want to pass the selected image in another activity and set it to linear layout.
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}
private void onSelectFromGalleryResult(Intent data) {
bitmap = null;
if (data != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
System.out.println("bitmap is :" +bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
set.setImageBitmap(bitmap);
}
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();
Intent i=new Intent(this,NextActivity.class);
i.putExtra("path",picturePath);
startActivity(i);
}
}
NextActivity File :-
String picturePath =getIntent().getStringExtra("path");
LinearLayout imageView = (LinearLayout) findViewById(R.id.imgView);
Bitmap bmImg = BitmapFactory.decodeFile(picturePath);
BitmapDrawable background = new BitmapDrawable(bmImg);
imageView.setBackgroundDrawable(background);
Used from https://stackoverflow.com/a/26403116/8603832
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && 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();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
if (bitmap != null) {
ImageView rotate = (ImageView) findViewById(R.id.rotate);
}
}
pass selectedImage URI in Other Activity and using intent and use same code for generate the Bitmap with that URI
I have this code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Button play= (Button) findViewById(R.id.play);
play.setVisibility(1);
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));
}
}
public void Play(View view){
int[] mImageIds = {R.id.imgView};
Intent intent = new Intent(this, PuzzleSelectActivity.class);
intent.putExtra("images", mImageIds);
this.startActivity(intent);
}
i want to get the image id from the protected void onActivityResult and pass it on the mImageIds...
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(bitmap);
}
}
Get Bitmap from an Uri [android]
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();
}
}
}
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.