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
}
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 tried picking a file from the internal or external storage with the code below:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 1);
Of course it has onActivityResult method, and it's not the problem. It works fine in the modern phones or phones that have file manager installed. But the old one with no file manager throws
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT typ=file/* }
I tried switching to ACTION_PICK but no luck. I also tried intent.setType("*/*");, it didn't crash but the popup ask for action (videos, contacts,...) which is not true. I just want to pick any file not just a specified type.
I don't want to use any other file manager just to pick a file. Is there anyway I can get through this?
I believe that having the error explained, makes the solution much easier. So let me explain it to you:
You're starting an implicit intent. That means it's an intent that you know what you want to happen (use select a file) and you don't care which application will do it.
The error you're encountering is simply the system telling you (the developer), that there's no application installed that is capable of doing it (neither system nor 3rd party). There's simply no one capable of handling the action you want.
So you have two options from what I can see:
try-catch the error
.
try {
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException e) {
// maybe you should show a toast to the user here?
Toast.makeText(context, "You need to install a file picker", Toast.LENGTH_SHORT).show();
// or maybe redirect to a 3rd party app that you know works
startIntent(new Intent(Uri.parse("https://play.google.com/... some app
}
you can find a library or code to pick the file from inside your own app: http://bit.ly/1N1fZbO
Below code section should work for you!
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
Intent intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
What's your Android version? Some android maybe not released with such activity.
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 am newbie to android and development world. I need help to solve this problem.(scenario as follows)
I have created one android app using HTML5 and CSS and bundle it out using phonegap.
Now I need to display PDF file in this app with two options for user whether first download and then read or second read online
So My problem is, I am not able to dispaly PDF in app.
How could I achieve this? please help . I am trying to solve it from last 4 days and tried out each and solution which is already given in forum , but still no success.
It would be great for me if someone ans step by step procedure or one example to refer.
Thank You
Minal
This may help: How to open a PDF via Intent from SD card
The basic idea is to get a pdf app to render the pdf for you, and your app just kicks off an intent to do that.
try this code
private void openBook() {
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
}
you can open pdf file via intent like this
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
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.