Adding Contextual Actionbar to file-opening Intent - android

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?

Related

How to simply open directory/folder?

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.

Android Custom FileChooser

In my app the user has the opportunity to import a file via an intent.
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(new Helper(FunctionsActivity.this).getPathToAppFolder());
chooseFile.setDataAndType(uri, "*/*");
chooseFile.setType("*/*");
Intent intent = Intent.createChooser(chooseFile, getString(R.string.importDatei));
startActivityForResult(intent, REQUEST_CODE_IMPORT_FILE);
The valid files are files with the ending: .tr
These can undestood by my app.
The problem is, that the Intent shows all files in the folder:
After long searching I could not find a solution to show only .tr files. (Besides: For .xml or known file formats it is possible.)
So my idea is to write a small custom filechooser. When you click on "import", an activity opens that shows the names of all files with the extension .tr
So my question is:
Has anyone implemented something like this and can help me or does anyone has tips how to implement this the best?
I am grateful for everything that helps me further

View folder contents in Android using default explorer

I want to programatically launch a default file explorer to show me the contents of a folder.
I'm using this code, but it crashes:
Uri startDir = Uri.fromFile(new File("/sdcard/DCIM/Camera"));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(startDir);
startActivity(intent);
LogCat shows "No Activity found to handle the Intent"...
What's my best option to do this? I want the user to see the contents of the folder and be able to click the files (e.g. click a video and launch it with default player, click a PDF and open it etc).
Unfortunately there seem to be no standard way to do this, I was searching for the exact same thing before and couldn't find out any solution. There are 2 alternative methods that might work for you:
1) Use a general intent and let the user pick his/her file manager
This is safe and easy but a little far from what we really want
Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/DCIM/Camera"));
Intent intent = new Intent();
intent.setData(startDir);
intent.setType("*/*");
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
Don't use intent.setType("file/*");, it's not a standard MIME type.
2) Use Specific Intents that the famous file managers provide The well known file managers have their own custom intent filters that accept a directory path which allows simple browsing. Some of them are here: OI file manager, ES Explorer
Maybe you could check if the user have these specific file managers installed and then use this solution, else fallback to general intent.
For now these are the only two options you have. I will update this post if I find out any better solution.
Although I could not get the OI file manager to go to a specific directory, the following opens up the OI File Manager directly and goes to the last directory it was in.
Intent importFileIntent = new Intent();
importFileIntent.setType( "file/*" );
// Does nothing.
// Uri startingDir = Uri.fromFile( new File(
// Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera"));
// importFileIntent.setData( startingDirectory );
importFileIntent.setAction( Intent.ACTION_GET_CONTENT );
// Ensure that there's an activity to handle the intent.
if (importFileIntent.resolveActivity(getPackageManager()) == null) return;
startActivityForResult(importFileIntent, REQUEST_FILE_IMPORT);
If anybody has further success, I would love to hear it.

android dev open a file through os

hi is there a way to ask the os to open a file with its predefined programm?
I do not know what kind of file it might be, maybe a photo , txt anything.
I want to issue a command to the os to ask it to open the file(i have the filepath) with any program the os wants. It might be the case that the os opens the dialog asking the user to select one program to open the file(that is fine with me).
thanks
You do that via an intent chooser:
please refer to : http://developer.android.com/training/sharing/send.html
This is done through an Intent You will need to set the action and the data.
//from inside an Activity
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.fromFile(theFileYouMentioned));
try{
startActivity(i);
}
catch(ActivityNotFoundException) {
//user doesn't have an app that can open this type of file
}

Android: Open pdf file with the specified software

Now I have downloaded a .pdf file from website and wanna open it with the specified software like "Adobe Reader".How can I achieve this?
You cant. All you can do is fire off an intent and let the system handle it. If the user already has a default app that they've chosen to handle PDFs, that's the one that will open the PDF.
In general it's something like:
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} .......
I have solved this problem by adding the method "Intent.setComponent(pkg,cls);".
PS: I don Not angry about downing my reps.And thank you for giving me the suggestions.
PSS: What I wanna say is "Not everyone will get the same apple." I post this question for I have this problem and it occured in Android App.And Someone told me "Cannot".I post my answer just to tell you Ive solved this(or just mine) problem,and maybe it will help someone who has the problem same as mine.Yeah,this answer may make no sense to you.
PSSS::-)But I still think the API make sense to me and to its Designer.
*PSSSS:*At last,thank you all.And forgive my poor English.
You should first have a look at the intents filters documentation. Then you can start an intent (after adding the intent filter) for your PDF file.

Categories

Resources