Java - How to pick multiple files in Android - java

I’m doing an application for Android (version 4.4), and I’m trying to select multiple files (.doc, .pdf) at the same time. The objective is to navigate through directories, select some desiderated files and return a list of these files.
I’ve initially tried in this way, but the instruction Extra_Allow_Multiple doesn’t work: I can only select one file at a time.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
So I’ve tried in this other way, but I’m not browsing in all directories of the device, only in specific ones (images/videos/audio…), and I can’t select
multiple files for the same reason as before.
Intent intent = new Intent();
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
I’ve tried also other combinations, but often an error similar to this appears:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT (has extras) }
How can I select multiple files, possibly in a way alike to first method?

Have you tried this?
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
I took this snippet of code from this StackOverflow question.
I am not very familiar with this functionality in Android, but you can apparently retrieve the files Uri with onActivityResult() if this helps any further.

Related

intent.extra.NOT_UNKNOWN_SOURCE not working in Android-11

I have an installer app which installs/replaces an existing app using the below code. Up till Android-10 it works. But not in Android-11. So far my searches did not say any deprecation of android.intent.extra.NOT_UNKNOWN_SOURCE. So it should have worked.
Any idea?
Code snippet installing the app.
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
intent.putExtra("android.intent.extra.RETURN_RESULT", true);
intent.putExtra("android.intent.extra.NOT_UNKNOWN_SOURCE", true);
intent.putExtra("android.intent.extra.ALLOW_REPLACE", true);
startActivityForResult(intent, REQ_INSTALL_APK);

Remove Contact select option form file select options

I am opening file pick Intent with, Bellow code
Intent intent_upload = new Intent();
intent_upload.setType("*/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
activity.startActivityForResult(intent_upload, Constants.FILE_PICK_REQUEST_CODE);
I Want remove Contact option from list, please can anyone help.
Thanks
Use below code I think it can help you and also refers Link
Intent intent_upload = new Intent();
intent_upload.setType("*/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
intent_upload.addCategory(Intent.CATEGORY_OPENABLE);
activity.startActivityForResult(intent_upload, Constants.FILE_PICK_REQUEST_CODE);
You need to specify what type of intent (apps) you want to open. Now you set offer me all apps:
intent_upload.setType("*/*");
This can be different type for images, music, documents etc. eg.:
intent.setType("image/*");
As CommonsWare said you don't have alternatives than setting the specific MIME_TYPES and ignore using "*/*". Use specified MIME_TYPES like here..
String[] mimetypes = {"image/*", "video/*"};
Intent intent_upload = new Intent();
intent_upload.setType("image/*,video/*");
intent_upload.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
MainActivity.this.startActivityForResult(intent_upload, Constants.FILE_PICK_REQUEST_CODE);

Android: How to launch call intent using google voice?

How to launch specific intent (such as call) using google voice? How to pass phone number using intent? Following code launches google voice but what value to be passed for making call using google voice as intent extras?
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.apps.googlevoice", "com.google.android.apps.googlevoice.activity.conversationlist.ConversationListActivity"));
intent.putExtra("label", "<phone number>");
startActivity(intent);
Here what should i put in label to start the intent that launches a call using google voice?
Any help is appreciated... Thanks in Advance...
NEVER target applications directly like that UNLESS it is in your package. You should be using the Intent filter to catch that particular application. Sometimes you have to target an application like this, but this brings up the risk of change in package name errors.
To handle your particular application, you need to look at how information is being passed into Google voice. this will give you insight and how to target it WITHOUT targeting the exact package name.
What #JoxTraex said makes sense. However some clients need funny features like this, so we have no way but to implement this:
try {
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + mobile));
intent.setPackage("com.google.android.apps.googlevoice");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
GMHintManager.getInstance().showError(context, "Google Voice not installed");
}
Yes, you should try-catch ActivityNotFoundException.

How to Invoke or call one app from another app in Android?

I want to invoke one application from another application.
My Java file code:
Intent intent = new Intent(Intent.ACTION_RUN);
intent.setComponent(new ComponentName("Package name", "class name"));
startActivity(intent);
But I'm getting problem in specifying exact package path and I don't know permission for that code in Manifest. Please, help me on this.
I am going to assume that you really mean that you want to launch another app, not another Activity in your app.
Then there are two ways to do this. You can try using an implicit intent which according to the docs, an (Implicit) intent is "an abstract description of an operation to be performed" that "provides for performing late runtime binding between code in different applications." Sort of like trying to launch a method over the wire using an interface. You cannot be sure exactly what the class of the object that is launched only that it can handle the action and categories that you declare.
The second approach is an explicit intent, which is more like making a concrete call over the wire. If you know the package and class name this should work.
Intent intent = new Intent(Intent.ACTION_MAIN);
//intent.putExtra("plain_text", "Testing");
intent.setClassName("packagename", "packagename.ClassName"); // Explicit Intent
try {
startActivity(intent);
}
catch (Exception e)
{
Log.d(TAG","onCreate",e);
}
}
You can add extra info using flags depending on your needs and where your are trying to launch from.
JAL
Starting an external activity from your app is done using a slightly different method to that which you are using. You need to create an intent with a given action. For example, launching an intent to fetch an image from the gallery would look like this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK);
Note that you don't explicitly define the activity to be loaded, rather the kind of action you want to perform. Android will then pick (or have the user pick) an activity that has registered to handle this kind of intent to be run.
You might need to be a little more specific about what you're doing. If all you want to do is, say, launch another Activity from your main Activity, something like this would work:
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", "data"); //put any data you want to pass into the new activity
startActivity(intent);
Then just make sure you put the new activity in your manifest like this:
<activity android:name=".OtherActivity"
android:label="#string/other"/>
If your goal is something else then you should be ore specific with what you want to do.

Debugging Intents

I asked a question previously about what shows up in the IntentChooser when I send an Intent with ACTION_SEND and MIME type "image/png". The problem is that some phones come with a default messaging app that is not showing up in the list, mine included (myTouch 4G) as well as a user that I speak with over email (using an HTC EVO). If I choose a Send or Share option from the built in gallery app or another application on the same image I'm saving and attempting to send directly from my app, Messages shows up in the list. From my app it does not. Other phones don't have this problem, so it's clearly a platform specific thing. But that doesn't mean I should just ignore the problem.
So, I go to troubleshooting the issue. I register one of the activities in my app to receive the the same type of intent, and then hit a breakpoint to analyze the Intent object being sent from the two different ways of sending it.
The problem is, the intent I'm sending and the intent being sent from Gallery or AndroZip (where Messages does show up in the chooser) seem to be the same. They both have the same action, same categories, same flags, same mime type. What else can I inspect on the Intent from Gallery or AndroZip to tell if there's some more information I can add to my Intent to get the default messaging app to show up in the chooser in cases where it is not?
The problem is specific to HTC Sense phones, and it arises because their Gallery and Messaging apps are different to the stock ones.
Specifically the Intent sent from Gallery to Messaging has the action android.intent.action.SEND_MSG which is different to android.intent.action.SEND. The Sense messaging app doesn't handle SEND, unlike the stock messaging app.
So the question becomes, how is the Sense Gallery app creating an activity chooser dialog which combines both SEND and SEND_MSG ?
I've done some research and got mostway there... the code below works, but the "Messages" entry in the dialog appears at the top rather than in alphabetical order as per Gallery. Doubtless some more research into intents would correct that, but at least this works:
// Create a chooser for things that can ACTION_SEND images
Intent intent = new Intent(Intent.ACTION_SEND);
Uri data = Uri.parse("content://media/external/images/media/98");
intent.putExtra(Intent.EXTRA_STREAM, data);
intent.setType("image/jpeg");
Intent chooser = Intent.createChooser(intent, "Blah");
// Add the stupid HTC-Sense-specific secondary intent
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.putExtra(Intent.EXTRA_STREAM, data);
htcIntent.setType("image/jpeg");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
// Show the chooser
startActivity(chooser);
First of all, +1 to Reuben, he is the genius, not me. But I had to modify his code a bit to get it to work. Basically I had to putExtra() on the htcIntent or the image never got stuck to the Intent.
Tested and validated on a Droid X and HTC Incredible (which had the same problem until now thanks to Reuben).
Uri uri = Uri.fromFile(new File(mFile));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Intent htcIntent = new Intent("android.intent.action.SEND_MSG");
htcIntent.setType("image/png");
htcIntent.putExtra(Intent.EXTRA_STREAM, uri);
Intent chooser = Intent.createChooser(intent, "Send Method");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { htcIntent });
startActivity(chooser);
Edit: I realize I'm putting the image on two Intents now, but I couldn't get it to work any other way.
Instead of debugging the intents, why not try to compare how your starting the chooser with how the gallery is doing it. It is open source after all, so instead of trying to guess at the issue with the result, you can debug from the cause.
https://android.googlesource.com/platform/packages/apps/Gallery3D

Categories

Resources