How to get image id from uri? - android

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]

Related

The picture is not being shown when clicked on addImage button. What is missing in the code?

I am trying to add Image from Gallery to show up in my app. But it is not showing after I select the image from the gallery. It is not showing any error and runs successfully on my device.
private static int RESULT_LOAD_IMAGE=1;
public void addImage(View view){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 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.memeImage);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
try this with uri
imageView.setImageUri(selectedImage )

How to set chosen image from gallery to linear layout using intent? [duplicate]

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

get selected image from gallery into imageview

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

How to load an image in image view from gallery?

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

Load photo in the gallery as bitmap object in Android

How can I load the images from the gallery as android Bitmap objects???
Try in your Activity:
private static final int SELECT_PICTURE_ACTIVITY_REQUEST_CODE = 0;
....
private void selectPicture() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
}
....
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
.........
}
cursor.close();
}
break;
}
}

Categories

Resources