Select picture from Gallery device - android

How can i get picture from gallery device and send with parameter to other intent.
First i need call this method:
public void selectImageFromGallery() {
try{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select an picture"), SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
And i have this in method StartActivityForResult
#Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
Uri selectedImage = intent.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);
cursor.close();
}
}
}
But not works! when i select an picture from gallery nothing happend.
I need select an picture from gallery device and to send other intent.
Somebody have some idea i thanks a lot.

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 )

Android fragment disappear when return from startactivityforresult

I have an android predefined navigation drawer activity named as MainActivity.
From that on addUser button click AddUserFragment is showed. There is a button addImage when i click on that it startAnActivityForResult. And from gallery when i pick the image instead of returning to AddUserFragment it take me back to MainActivity.
Thanks in advance!.
for pick image from fragment start intent in this way:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
and handle the result in this way:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = imageReturnedIntent.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);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
NOTE: do all command in fragment

Place a 2D image on the top of the background

I am working on the android application with following requirements
Take images from gallery and display it as background.
This is done as follows:
btn_select.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),PICK_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requestCode && 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);
imageView.setImageURI(Uri.parse(picturePath));
cursor.close();
}
2.Place a 2D iamge on the top of the background image.
3.Reposition the image by dragging
4.Scale the image.
Anyone please suggest the ways to complete steps 2,3 and 4.
Thanks in Advance!

how to get the Image from Gallery and set into Image view in Fragments?

I have Written code for getting the Image from gallery and set into the Image View but the image is not set.This is my problem suggest me guys
Here is My Code:
btnUpload.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);
getActivity().startActivityForResult(i, 1);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && null != data)
{
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);
//int pic=Integer.parseInt(picturePath);
cursor.close();
//ImageView imageView = (ImageView) mFormView.findViewById(R.id.imageView);
ivMan.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
thanks in Advance
Replace getActivity().startActivityForResult(i, 1); with
startActivityForResult(i, 1);
for getting image from gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Check if you have a proper path if yes than decode the image and set to imageview.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
cursor.close();
File file = new File(imageFilePath);
if (file.exists()) {
Bitmap bMap = decodeFile(file);
// asset.setImageBitmap(bMap);
asset.setImageBitmap(bMap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
this will defiantly help you

Android - Image Picker, Wrong Image

I am starting a request for an image pick:
Intent intent = new Intent();
intent.setType( "image/*" );
intent.setAction( Intent.ACTION_GET_CONTENT );
startActivityForResult( Intent.createChooser( intent, "Choose"), PHOTO_GALLERY );
And getting the data back out in onActivityResult:
if( resultCode == Activity.RESULT_OK && requestCode == PHOTO_GALLERY )
{
U.log( data.getData() );
Bitmap bm = ... // built from the getData() Uri
this.postImagePreview.setImageBitmap( bm );
}
When I launch the Intent, I see some folders, such as sdcard, Drop Box, MyCameraApp, and so on.
If I chose a picture from sdcard, when I load the preview, it is the completely wrong image. The other folders don't seem to be giving me this problem.
Does anyone know why it'd let me pick one image, then give me the Uri for another?
EDIT: Here are some exampled logged getData()s:
Good:
content://com.google.android.gallery3d.provider/picasa/item/5668377679792530210
Bad:
content://media/external/images/media/28
EDIT: I'm still having issues, when picking from the sdcard folder of gallery.
Here is a bit more expansion of what I'm doing in onActivityResult:
// cursor
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = mContext.getContentResolver().query( selectedImage, filePathColumn, null, null, null );
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex( filePathColumn[0] );
String filePath = cursor.getString( columnIndex );
cursor.close();
// Cursor: /mnt/sdcard/Pic.jpg : /mnt/sdcard/Pic.jpg
U.log( "Cursor: " + filePath + " : " + Uri.parse( filePath ) );
// "regular"
// Regular: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular: " + data.getDataString() + " : " + Uri.parse( data.getDataString() ) );
// Regular 2: content://media/external/images/media/28 : content://media/external/images/media/28
U.log( "Regular 2: " + data.getData() + " : " + data.getData() );
mPostImagePreview.setImageBitmap( BitmapFactory.decodeFile( filePath ) );
mPostImagePreview.setVisibility( View.VISIBLE );
They still set the wrong image. If I go into the Gallery, long press the image, and view its details I get:
TItle: Pic
Time: May 2, 2012
Width: 720
Height: 1280
Orientation: 0
File size: 757KB
Maker: Abso Camera
Model: Inspire 4G
Path: /mnt/sdcard/Pic.jpg
So, the Gallery is telling me the path is the same as the pick action, and the Gallery is rendering it correctly. So why on earth is it not rendering if I set it from onActivityResult?
Also, this is the code I'm using to fire the Intent now:
private void selectPhoto()
{
Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
intent.setType( "image/*" );
( ( Activity )mContext ).startActivityForResult( Intent.createChooser( intent, "Select Picture" ), PHOTO_GALLERY );
}
Sometimes the thumbnails in the gallery app can be outdated and show thumbnails for a different image. This can happen when the image ids are reused, for example when an image gets deleted and a new one is added using the same id.
Manage Apps > Gallery > Clear Data can fix this problem then.
This is the code to open gallery. However this the same what you have done. Also see the onActivityResult code which I used to retrive the selected image.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PHOTO_GALLERY);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImageUri = Uri.parse(data.getDataString());
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
getApplicationContext().getContentResolver(),
selectedImageUri);
this.postImagePreview.setImageBitmap( bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
private static int RESULT_LOAD_IMAGE = 1;
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
OnActivity Result
#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));
}
}
try this one
//Put this code on some event
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
// When above event fire then its comes to this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK && requestCode==1){
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]);
filePath = cursor.getString(columnIndex);
cursor.close();
// Use it as per recruitment
actualBitmap =BitmapFactory.decodeFile(filePath);
}
}
Try this,
public class SelectPhotoActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SELECT_PICTURE);
}
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);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
// here you can set the image
}
}
}
}

Categories

Resources