how to open the gallery? - android

I need to open the images gallery via code in my app.(only opening the gallery, the user is not going to select any image). I searched and found lots of ways but some of them worked only for selecting an image and other ways never worked.e.g.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
or
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
how may I open the gallery?

public void pickPhoto(View view)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
Here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
Uri curImageURI = data.getData();
Bitmap bit = getRealPathFromURI(curImageURI);
imageView.setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
and
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
android.database.Cursor cursor = managedQuery(contentUri, proj, null,
null, null);
int column_index;
try {
column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return null;
}
}
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is the final number in the "startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);"
in this case '1' so you'd need to change it to 1 or declare it as a global variable

Related

In Android how to open system built in gallery app using intent?

In my application I am trying to open only default built in gallery app not even photos app and other file explorer apps. On button click it will directly land in gallery,How can I do this?
My Code
'Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,PICK_IMAGE);'
Try Like this
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE);
OnActivityResult for get image
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE)
{
if (resultCode == Activity.RESULT_OK)
{
if (data != null)
{
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e)
{
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED)
{
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_SHORT).show();
}
} }
Add permissions in Manifest File
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Try this one...
private int PICK_IMAGE_REQUEST = 1;
tvGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
Use Permission in Android Manifest File
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try this:-
public static final int GALLERY_PICTURE = 1;
private String selectedImagePath = null;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), GALLERY_PICTURE);
onActivityResult() :-
if (requestCode == GALLERY_PICTURE && resultCode == RESULT_OK) {
selectedImagePath = getRealPathFromURI_API19(this, data.getData());
Log.e("gallery path", selectedImagePath);
}
#SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}

Select picture from Gallery device

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.

android - getpath method returns incorrect address

I used startActivityForResult to pick a picture from gallery and then used onActivityResult for bring the result.
when i use getPath() for the intent result to send the path to another activity to set the source of that picture , the path is incorrect and is another way that the picture is not located there .
the picture is located in sdcard and located at : "mnt/sdcard/pictures/lambo"---- but getpath() his returns : "external/images/media/17
photopicker :
private void photopicker() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);
}
onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String add;
add = selectedImage.getPath(); // don't work
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selectedPhoto = BitmapFactory.decodeStream(imageStream);
// add = selectedImage.getPath(); // don't work
Intent intent = new Intent(MainActivity.this, PicViewer.class);
intent.putExtra("add", add);
startActivity(intent);
}
}
}
You can try this:
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Native video gallery does not display all the videos

My application filmed a video and then back to a screen that allows me to choose another video or gallery display videos.
The problem is that the gallery does not display all the videos I record, just one. I'm sure I record the videos because they appear in My Files.
The code used to display the gallery is as follows:
final static int REQUEST_VIDEO_CAPTURED = 1;
private Uri uriVideo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video_list);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("video/*");
startActivityForResult(intent, 1);
//I also try this
// Intent intent = new Intent(Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// intent.setType("video/*");
// startActivityForResult(intent, 1);
// and this
// Intent intent = new Intent();
// intent.setType("video/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent.createChooser(intent, "Select Video"),
// 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_CAPTURED) {
uriVideo = data.getData();
Toast.makeText(VideoList.this, uriVideo.getPath(), Toast.LENGTH_LONG).show();
ContentResolver res = getApplicationContext().getContentResolver();
Cursor cursor = MediaStore.Video.query(res, data.getData(), new String[] { MediaStore.Video.VideoColumns.DURATION });
if (cursor.moveToFirst()) {
duration = cursor.getString(0);
}
if (uriVideo != null) {
}
}
} else if (resultCode == RESULT_CANCELED) {
uriVideo = null;
finish();
}
}
I hope you understand, it's my first question.
Thank you very much!
Sorry for my bad English
you must request the system to update the media gallery

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