The problem I have is that when this intent is started there are no apps to show. Why is that? I need all apps to be visible since onClick the button will go to any app the user wants to and save it (use this app as default). What could be the solution?
Intent intent = new Intent(Intent.ACTION_ALL_APPS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent, title);
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(chooser);
Try-
Intent pickIntent = new Intent(Intent.ACTION_ALL_APPS);
pickIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
pickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(pickIntent);
This will give you all the apps in a list-
Intent pickIntent = new Intent(Intent.ACTION_MAIN, null);
pickIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = getPackageManager().queryIntentActivities( pickIntent, 0);
Log.d(MainActivity.class.getSimpleName(), pkgAppsList.toString());
//startActivity(pickIntent);
pkgAppsList will have the list of all the applications. Hope this solves your problem.
Related
I am trying to access photo libraray in android device using this code:
txtSelectPhoto.setOnClickListener(v->{
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i,GALLERY_CHANGE_PROFILE);
});
but when it opens it just gives me a blank screen of the device folders with no photos like this although there are photos on the device.. is there any way to solve this?
Remove setType() and instead pass a Uri in the Intent constructor, representing the collection that you want the user to pick from:
txtSelectPhoto.setOnClickListener(v->{
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_CHANGE_PROFILE);
});
Or, keep the MIME type and switch to ACTION_GET_CONTENT:
txtSelectPhoto.setOnClickListener(v->{
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, GALLERY_CHANGE_PROFILE);
});
I'm developing an application that displays .dxf files (using Kabeja library). I've done all that part and everything works just as I wanted to. Problem is, I need to have a button that opens a file browser so the user can import his own .dxf from the sd card or local storage. It needs to filter files to only display .dxf so no other extension can be imported. I have absolutely no idea how to do this, nor to make a basic file browser. Could you help me getting on the right track ?
Thanks
Try this
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 1);
}
And if you want to enforce an app chooser
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
Intent chooser = Intent.createChooser(sendIntent, "Title");
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(chooser, 1);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); // all images
//intent.setType("image/png"); // only pngs
startActivityForResult(intent);
And get response in onActivityResult method. More details in documentation.
I'm working on a app-drawer replacement but I'm having trouble giving the user the ability to create home screen icons.
I can create the shortcuts, but I'm struggling to pull the icons from the respective apps.
I collect information on the apps using:
manager = getPackageManager();
apps = new ArrayList<AppDetail>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> availableActivities = manager.queryIntentActivities(i,0);
Collections.sort(availableActivities, new ResolveInfo.DisplayNameComparator(manager));
for(ResolveInfo ri:availableActivities){
AppDetail app = new AppDetail();
app.name = ri.activityInfo.packageName;
app.label = ri.loadLabel(manager);
app.icon = ri.activityInfo.loadIcon(manager);
app.iconID = ri.getIconResource();
apps.add(app);
But I'm running into troubles when I try to create the shortcut
AppDetail selectedApp = apps.get(info.position);
...
Intent shortcutIntent = new Intent(this, selectedApp.getClass());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, selectedApp.label);
// the danger zone
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getBaseContext(),selectedApp.iconID));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
finish();
How can I correctly reference the icon file of another app?
Please use
Drawable icon = manager.getApplicationIcon(app.name);
to obtain the icon graphics, and
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, selectedApp.label);
// this line in your code changed
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon.getBitmap());
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
When I create a shortcut for my application, and when I want to delete shortcut with code, how should I do it? I try some method but not used.
The create shortcut code:
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test");
shortcut.putExtra("duplicate", false);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, SecondActivity.class);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);`
The delete shortcut code:
actionIntent.addCategory(Intent.CATEGORY_LAUNCHER);
actionIntent.setClass(this, SecondActivity.class);
Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "taotao");
intent.putExtra("duplicate", false);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
sendBroadcast(intent);`
In my application after button click I want to show chooser of available photo applications in my app. The goal is to pick image from one of the photo-related apps and show it in my app.
After button click following method is being executed:
private void fireIntentToOpenDeviceImageResources() {
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContext().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent, takePicture});
startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST);
}
The problem is that I don't see dropbox app (it is for sure installed in my testing phone) in created chooser. What kind of intent I should add in order to include dropbox app?
EDIT:
I added such intent:
PackageManager manager = getActivity().getPackageManager();
Intent i = manager.getLaunchIntentForPackage("com.dropbox.android");
i.setAction(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Now it shows dropbox app in chooser but I can't pick the image from it. It simply launches dropbox app without picking and returning to my app possibility.
Android 4.4 (API level 19) introduces the Storage Access Framework (SAF), with a centralized document access. You can open to the document chooser with this simple code:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
startActivityForResult(getIntent, MY_REQUEST_CODE);
For this reason on Android >=19 your code will open the intent chooser with various app, for example Camera, Photos (etc, etc... depends on what apps are installed) and Documents. If you select Documents the document chooser will open and on the sidebar there are all the different apps installed in your device, including Dropbox.
If you want that Dropbox will be directly available in the intent chooser you can change your code like this:
private void fireIntentToOpenDeviceImageResources() {
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
Uri imageUri = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Intent dropboxIntent = new Intent(Intent.ACTION_GET_CONTENT);
dropboxIntent.setPackage("com.dropbox.android");
dropboxIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent});
startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST);
}
Warning! With this technique you need to manually add every app that it was available only in the document chooser, for example if you want to add also ES File Manager you need to create the intent and add to the list of EXTRA_INITIAL_INTENTS:
Intent esFileManagerIntent = new Intent(Intent.ACTION_GET_CONTENT);
esFileManagerIntent.setPackage("com.estrongs.android.pop");
esFileManagerIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent, esFileManagerIntent});