How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?
The following code gives a list of activities that can select an image:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
List<ResolveInfo> infos = activity.getPackageManager().queryIntentActivities(intent, 0);
If I then do something like this:
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE);
then if the user has more than one application that can act like a gallery (such as ASTRO File Manager), s/he is prompted to select one of them, with no "set as default" option.
I don't want the user to be prompted to choose between them each time, so I'd like to just use the native gallery.
The hacky code sample below uses a whitelist to test for known native gallery names:
for (ResolveInfo info : infos) {
if ( 0==info.activityInfo.name.compareTo("com.cooliris.media.Gallery")
|| 0==info.activityInfo.name.compareTo("com.htc.album.CollectionsActivity")
) {
// found the native gallery
doSomethingWithNativeGallery();
}
}
Feels kinda dirty. Is there a better way? I suspect I'm missing something in my intent.
I didn't get last part of your code.
To start the native gallry what i did is -
public void upload(){
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/jpg");
photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
startActivityForResult(photoPickerIntent, 1);
}
Call the upload() where you like to start the native gallery.
Then to get that image info i did -
/**
* Retrieves the returned image from the Intent, inserts it into the MediaStore, which
* automatically saves a thumbnail. Then assigns the thumbnail to the ImageView.
* #param requestCode is the sub-activity code
* #param resultCode specifies whether the activity was cancelled or not
* #param intent is the data packet passed back from the sub-activity
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
return;
}
else if(resultCode == RESULT_OK) {
Uri uri = intent.getData();
String path = getPath(uri);
Log.i("PATH", path);
data = path;
return;
}
uplad();
}
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);
}
If you wouldn't get your answer, clear what you like to do
Related
I am trying to fetch a file this way:
final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity
.getApplicationContext().getPackageManager()) != null) {
chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}
Then in onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :
/document/acc=1;doc=28
So what to do? Thanks for your help.
I am trying to fetch a file
Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()
That was never correct, though it tended to work on older versions of Android.
So what to do?
Well, that depends.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT. (UPDATE 2019-04-06: since Android Q is banning most filesystem access, this solution is no longer practical)
If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:
If getScheme() of the Uri returns file, your original algorithm will work
If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user
To get the real name and to avoid getting a name that looks like "image: 4431" or even just a number, you can write code as recommended by CommonsWare.
The following is an example of a code that selects a single pdf file, prints its name and path to the log, and then sends the file by email using its uri.
private static final int FILEPICKER_RESULT_CODE = 1;
private static final int SEND_EMAIL_RESULT_CODE = 2;
private Uri fileUri;
private void chooseFile() {
Intent fileChooser = new Intent(Intent.ACTION_GET_CONTENT);
fileChooser.setType("application/pdf");
startActivityForResult(Intent.createChooser(fileChooser, "Choose one pdf file"), FILEPICKER_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILEPICKER_RESULT_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data != null ? data.getData() : null;
if (fileUri != null) {
DocumentFile d = DocumentFile.fromSingleUri(this, fileUri);
if (d != null) {
Log.d("TAG", "file name: " + d.getName());
Log.d("TAG", "file path: " + d.getUri().getPath());
sendEmail(fileUri);
}
}
}
}
}
private void sendEmail(Uri path) {
String email = "example#gmail.com";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "PDF file");
String[] to = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, "This is the pdf file...");
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."), SEND_EMAIL_RESULT_CODE);
}
hope it helps.
I am trying to fetch a file this way:
final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String[] mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity
.getApplicationContext().getPackageManager()) != null) {
chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}
Then in onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :
/document/acc=1;doc=28
So what to do? Thanks for your help.
I am trying to fetch a file
Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.
According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()
That was never correct, though it tended to work on older versions of Android.
So what to do?
Well, that depends.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT. (UPDATE 2019-04-06: since Android Q is banning most filesystem access, this solution is no longer practical)
If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:
If getScheme() of the Uri returns file, your original algorithm will work
If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user
To get the real name and to avoid getting a name that looks like "image: 4431" or even just a number, you can write code as recommended by CommonsWare.
The following is an example of a code that selects a single pdf file, prints its name and path to the log, and then sends the file by email using its uri.
private static final int FILEPICKER_RESULT_CODE = 1;
private static final int SEND_EMAIL_RESULT_CODE = 2;
private Uri fileUri;
private void chooseFile() {
Intent fileChooser = new Intent(Intent.ACTION_GET_CONTENT);
fileChooser.setType("application/pdf");
startActivityForResult(Intent.createChooser(fileChooser, "Choose one pdf file"), FILEPICKER_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILEPICKER_RESULT_CODE) {
if (resultCode == RESULT_OK) {
fileUri = data != null ? data.getData() : null;
if (fileUri != null) {
DocumentFile d = DocumentFile.fromSingleUri(this, fileUri);
if (d != null) {
Log.d("TAG", "file name: " + d.getName());
Log.d("TAG", "file path: " + d.getUri().getPath());
sendEmail(fileUri);
}
}
}
}
}
private void sendEmail(Uri path) {
String email = "example#gmail.com";
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "PDF file");
String[] to = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, "This is the pdf file...");
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."), SEND_EMAIL_RESULT_CODE);
}
hope it helps.
I am in trouble displaying customized phone gallery.
At first, I need to show images folders in gridview (such as phone gallery), and on selecting any of the folder, it must show the pictures inside it and should allow multiple selection, so that I can pick multiple pictures.
Is it achievable?
If yes, how to do that?
To open gallery :
//open gallery to choose image
private void captureImage() {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
When the image is selected from the gallery , the following function is invoked , implement it to save the image selected .
// When Image is selected from Gallery
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
// Get the Image's file name
String fileNameSegments[] = imgPath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// successfully selected the image
// launching upload activity
tvCapturePicture.setText(fileName);
} else {
Toast.makeText(this, "You haven't picked any Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong ,please try again , ",
Toast.LENGTH_LONG).show();
}
}
as per your requirement follow this tutorial it will give ideas..
http://www.technotalkative.com/android-select-multiple-photos-from-gallery/
I have been researching on this but I am not able to find an answer for this.
I am picking an image from the gallery using media store intent and I am able to get the image file path in onActivityResult method. (I know how to get the URI in the intent and filepath).
I am passing in some intent extras on starting the activity (startActivityForResult) but all the intent extras are null.
Code snippets (in case):
This is my onActivityResult code which is working and i get the image path
/* On activity result from image button */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Result Code" + resultCode);
if(requestCode == FavoriteListAdapter.IMAGE_PICK_CODE && data != null && data.getData() != null && resultCode == FragmentActivity.RESULT_OK) {
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
String imageFilePath = cursor.getString(0);
System.out.println("imagefilepath" + imageFilePath);
System.out.println(data.getStringExtra("exp"));
cursor.close();
}
}
I am starting my activity with startActivityForResult
Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageIntent.setType("image/*");
imageIntent.putExtra("exp", "testing");
((FragmentActivity)view.getContext()).startActivityForResult(imageIntent, IMAGE_PICK_CODE);
I should be able to get the string "testing" in onActivityForResult but all I get is null.
Any ideas and suggestions will be appreciated. THnkas a lot.
Actually I figured it out .. When you are sending an intent to a system activity like MediaStore or the camera etc... the onActivityResult will not have the intent extras you sent while calling the activity.
This is probably by design and will only contain the extras given by the system activity. For instance after picking an image from the gallery, the returning intent from the gallery will only the URI containing the image path.
Same goes to camera or any system activites.
I want to play a video file recorded to be played in the mediaplayer of android.I want to call the media player through the intent and want to play the corresponding file of the passed uri.When I was trying I am getting an exception ActivityNotFound can anyone help me with a code.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_CAPTURED) {
uriVideo = data.getData();
Toast.makeText(VedioRecording.this, uriVideo.getPath(),
Toast.LENGTH_LONG).show();
}
} else if (resultCode == RESULT_CANCELED) {
uriVideo = null;
Toast.makeText(VedioRecording.this, "Cancelled!", Toast.LENGTH_LONG)
.show();
}
if (requestCode == 2) {
selectedImageUri = data.getData();
// OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
Intent intent1 = new Intent(android.provider.MediaStore.INTENT_ACTION_MUSIC_PLAYER).setData(selectedImageUri);
startActivityForResult(intent1, 3);
// videoviewPlay.setVideoURI(selectedImageUri);
// videoviewPlay.start();
}
if (requestCode == 3) {
}
}
private String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else {
return null;
}
}
}
this is my code i am geting activitynotfound exception
The most common scenario in which you get an ActivityNotFound exception is when you attempt to launch an Activity you have created without declaring it in the manifest.
Post your code that you use to launch the Activity to be sure. If you're trying to use an activity that should be provided by the framework externally from your application, you may just be setting up the Intent incorrectly
Update after code posted...
Your code seems to be using the intent action INTENT_ACTION_MUSIC_PLAYER and passing an image url as data (is it the path to an image or are your variables just misnamed?). You get an ActivityNotFoundException because the system doesn't have any intent receivers registered to handle that scenario. Also, if you look at the documentation for this constant, you'll see that they marked it deprecated at some point:
http://developer.android.com/reference/android/provider/MediaStore.html#INTENT_ACTION_MUSIC_PLAYER
I would normally use Intent.ACTION_VIEW and pass a mime type along with the data. Something like the following...
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(pathToVideoFile), "video/*");
startActivity(intent);
By passing the mime type of "video/*" to setDataAndType, you're being more specific with your request to the system.
If you want to query the system to find out if an Intent can be handled (meaning that the user's device running your code has an Activity registered that can handle the Intent), you can use the PackageManager.queryIntentActivities method:
queryIntentActivities
String extension = MimeTypeMap
.getFileExtensionFromUrl(selectedImagePath);
String mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension);
Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
mediaIntent.setDataAndType(Uri.parse(selectedImagePath),
mimeType);
startActivity(mediaIntent);
this is the code that helped me