I have a application requirement in Android. In my application I want to add files as attachments. This is to have it for quick reference.
In my layout I want to have a attach button. If the user clicks on the attach button he should get a file browser to browse the SD CARD. He must be able to select a file to attach.
I am not sure where to start of this code. I have placed a button and have a on click listener. But Inside the on click listener I am not sure what has to be done. Can you help me with some sample code or some links that can help me do it. Any kind of help is appreciated. Thank you.
You need an Intent to open up a file chooser. This is assuming the user has a file chooser.
int reqCode = 1;
Intent action = new Intent(Intent.ACTION_GET_CONTENT);
action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(action, reqCode);
Note reqCode is like a 'key' you use later. Next you'll override onActivityResult() in your Activity. When it runs, check to see if the requestCode matches your reqCode. If it doe, you know it's from your intent, and can get the data packed in the incoming intent.
This is just a brief overview, you should probably read up more on intents in the official documentation.
Related
DONT mark this as duplicate, before reading what I need.
I have seen many similar topics, but in none of them I've found solution.
I need the simplest thing: In my application I have button "View Media Files". After clicking that button, i need to be opened (with built-in File Explorer) this directory - SD_CARD/my_folder where are media files (and I want to click any of them and they should be opened in default Media player)..
I have used all suggested answers on SO , like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("/sdcard/Recorder_Videos");
intent.setDataAndType(mydir, "*/*");
startActivity(intent);
but all they do: after clicking button, it opens "Choose File" menu:
(Where I cant still play media files when clicking)
The solution (not complete) I have found, was that I was missing file:// prefix. Here is my solution (however, it shows all kinds of applications on first view):
public void openFolder(String location)
{
// location = "/sdcard/my_folder";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file://"+location);
intent.setDataAndType(mydir,"application/*"); // or use */*
startActivity(intent);
}
p.s. Strange and surprising, but there doesnt exist the standard definition of "File Browser" in stock Android systems (unless you install 3rd party "File Explorer")..
That's why "resource/folder" mime-type doesnt work by default..
However, let's say a simple truth. File-Browser is a SIMPLE and ESSENTIAL part of any OS system. And it's quite disrespectful from Android, saying that it's not their job, and throwing the responsiblity to 3rd party apps.
You can use type DocumentsContract.Document.MIME_TYPE_DIR which works on several devices and launches File Explorer. You can refer this SO for more details.
I'm going to develop a simple app to list .jpg files and after a click call an Intent.ACTION_VIEW, below the code:
File imageFile = new File(filename);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(imageFile), "image/jpeg");
startActivity(i);
The intent works properly, in fact in app I receive the message to select an app to show the selected image. But I noticed that after the choice, for example with Google Photos, not all function are enabled. For example the share function is not visible, has someone already noticed this behavior?
Some other details; if I choose Google Photos I only have the "Info" and "Guide and Feedback" options. If I choose gallery app (I have a Samsung Ace 3) I have only "Info" and "Set as wallpaper" options.
Note: if I open the same image directly from Google Photos all functions are enabled...
The ACTION_VIEW intent calls another application to view the content that you present, in your case a JPEG image. If there is more than on application that can handle that Intent, Android lets the user choose an app to display the image. Once the application receives the Intent, it is up to that application to display the content how it sees fit.
Basically, you have no control over what functions are available with Intent.ACTION_VIEW. If you want more control, you can create your own Activity in which you can view the image and provide whatever functions you would like.
The problem is I have an article that want to share to other apps, and I want to let the user to choose which app to share to. What I want to share is basically:
the title of the article
the URL of the article
the article content as HTML
the URL with some extra text (such as 'http://foo.com/article share from #FooApp')
All of these fields are optional, but I want to share at least one of them.
Such as when share via SMS or twitter, I want to set the content to part 4. when share via Facebook, I want to set 1, 2, 3 together. And when share via email, I want to set subject as 1 and message as 4.
I know (correct me if I'm wrong) every target intent receiver has it's own logic to pick up the fields it needed. So I want to provide as much information as possible and I wrote the following code:
String message = article.getURL() + " #FooApp";
Intent intent = new Intent().setData(Uri.parse(article.getURL())
.putExtra(Intent.EXTRA_SUBJECT, article.getTitle())
.putExtra(Intent.EXTRA_TEXT, message)
.putExtra(Intent.EXTRA_HTML_TEXT, article.getHTML())
.putExtra("sms_body", message)
...
.setType(HTTP.PLAIN_TEXT_TYPE);
startActivity(Intent.createChooser(intent, "Share to"));
But the problem is, it seems like a trick between setData, putExtra, setType.
For some apps appear in the chooser dialog, when I choose, the confirm share window (of that app) display nothing that I set to the intent. (for some other apps they just say failed to fetch resource)
For the putExtra part, when I add or remove some putExtra code, the target intent receivers diff a lot than I expected.
So the question is: am I doing it the wrong way? Are there some guideline for this problem?
As always, this is a bit difficult to explain so I hope you can all follow.
I have an Activity that allows people to search through their file system for a file they'd like to upload to a server. What I want, is for when a person clicks a file, that it'll open the file so the user can see if it is the right one/edit it/ect and then click accept to send it or decline to return to the file system search.
Here is what I have so far:
File selectedFile = new File(o.getPath());
Uri fileUri = Uri.fromFile(selectedFile);
Intent i = new Intent(FileChooser.this, FileView.class);
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(selectedFile), "text/*");
startActivity(i);
I'm able to open the files no problem when I don't try to call FileView.class, of
course. But then I'm not able to add the menu...I feel like there's something simple I'm missing.
Anyone have any experience doing this?
I searched the Internet for quite a while without finding a solution.
I want to let the user pick images of his gallery using an intent.
For this i use a GET_CONTENT intent:
Intent intentBrowseFiles = new Intent(Intent.ACTION_GET_CONTENT);
intentBrowseFiles.setType("image/*");
intentBrowseFiles.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intentBrowseFiles,1);
But this only works for one image. Is there any possibility to make it work for more than one, or do I have to rebuild my own gallery for this?
The documentation clearly says:
Output: The URI of the item that was picked. This must be a content: URI so that any receiver can access it. (emphasis mine)
So, no, there doesn't seem to be a way to use the default Intents to select multiple items. Having an "add more" button should be easier than developing your own gallery, though.