I got a activity that holds a path to a file (with unknown mime type).
I got 2 buttons. The one should open the folder in a file manager choosen by user.
The other should open the file in a application that fits to the filetype.
Clicking buttons work. openFolder let me choose a filemanager: using androzip or root explorer takes me to sdcard but astro wants me to choose a file as if i upload a file to a oneclickhoster. OpenFile gives me no choise but opens a hex Editor which unfortunatli says the file is "null".
public void openFolder(View v)
{
File f=new File(path);
if(f.exists())
{
String folder=f.getAbsolutePath().replace("/"+f.getName(),"")+"/";
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(folder);
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Open folder"));
}
}
public void openFile(View v)
{
File f=new File(path);
if(f.exists())
{
//example for path: path=/storage/sdcard0/test.mp3
Intent intent = new Intent(Intent.ACTION_EDIT);
Uri uri = Uri.parse("file:/"+path);
intent.setDataAndType(uri, getMIME());
startActivity(Intent.createChooser(intent, "Open file"));
}
}
private String getMIME()
{
String type =null;
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
if(type.equals("") || type == null)
return "UNKNOWN";
else
return type;
}
EDIT: the problem for opening the file was Intent.ACTION_EDIT, it should be Intent.ACTION_VIEW.
But I still don't know how to open the folder.
Related
I'm trying to open the download directory in my emulator using Intent.ACTION_GET_CONTENT.
I can successfully get the path of the downloads directory and list all the files in it, but I can't seem to open it with an intent. It only displays the Recent directory.
Successfully logs all the files in that download directory, so the path to it is not incorrect
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
File[] files = f.listFiles();
if (files != null) {
for (File inFile : files) {
Log.d(TAG, "onPermissionGranted: File name: " + inFile.getName());;
}
}
But when using the intent:
public void openDirectory() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
Log.d(TAG, "openDirectory: " + uri);
//logs this: /storage/emulated/0/Download
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Select keystore"));
}
It always displays Recent
Instead of Downloads
I have to manually go to the Downloads tab
How can I make it so that the intent goes to the Downloads without having the need to switch?
This is how you do it:
Intent intent=new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(intent);
Just want to confirm that is it possible that when i click on file of any extension will open up with its compatible software in android phone or display me the list of software’s present in mobile which can open the file and if it didn't found any software it will indicate user to first download the software to open that particular file (All this thing need to be done pro grammatically).
Thanks.
Any help will be appreciated.
In order to open the file you can use the following method, If there is no application that can handle given file, it simply shows a Toast saying no application found.
private void viewFile(String filePath, String title, int fileType) {
Uri uri = Uri.parse("file://" + filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
String dataAndType = getIntentDataAndType(filePath);
intent.setDataAndType(uri, dataAndType);
intent.putExtra(Intent.EXTRA_TITLE, title);
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "No Application found", Toast.LENGTH_SHORT).show();
}
}
UPDATED :
For finding the mime type of the file.
private String getIntentDataAndType(String filePath) {
String exten = "";
int i = filePath.lastIndexOf('.');
// If the index position is greater than zero then get the substring.
if (i > 0) {
exten = filePath.substring(i + 1);
}
String mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(exten);
mimeType = (mimeType == null) ? "*/*" : mimeType;
return mimeType;
}
I've coded my own file explorer and now I want to start to Implement capabilities to open various files. I have implemented Mime Types and all other things but how can I parse specific selected file to specific "viewer activity"? I know by using intents but how to receive it in that viewer app and use it. Many thanks guys :)
Something like this should work. Change file to whatever you want.
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
int index = file.getName().lastIndexOf(".") + 1;
String extension = null;
if (index > 0) {
extension = file.getName().substring(index);
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri u = Uri.parse("file://" + file.getAbsolutePath());
String mimeType = null;
if (extension != null) {
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
if (mimeType == null) {
mimeType = "*/*";
}
intent.setDataAndType(u, mimeType);
try {
startActivity(Intent.createChooser(intent, "Open with..."));
} catch (ActivityNotFoundException e) {
// handle the exception
}
I wish I could open any file type using mobile applications.
Here is the code :
File mFile = new File( file.getLocalPath() + file.getFileName() );
Log.i("path", file.getLocalPath() + file.getFileName());
if( mFile.exists() )
{
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(mFile.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
Log.i("type", type);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(mFile);
Log.i("uri", data.toString());
intent.setDataAndType( data, type );
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
}
}
Logs :
Path : /data/data/com.package.name/files/sbx/523/filename.jpg
Type : image/jpeg
Uri : file:///data/data/com.package.name/files/sbx/523/filename.jpg
All logs are displayed therefore the file exists.
However, during the opening of the gallery, I get an "Unable to find item".
Where is the problem ? Are there other solutions ?
The problem is that you are trying to open file which is in android private area thats why during the opening of the gallery, you get an "Unable to find item". You have to put your file in local. If you want to open a file of android private area you should be rooted.
I have written a piece of code that detects installed application in android and opens the file with the application.For example, a word document should be opened with some office apk.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(temp_file);
String type = getMimeType(temp_file.getName());
intent.setDataAndType(data, type);
this.startActivity(intent);
In the above code temp_file is the file that should be opened.And below is the generalised code that I wrote to get the MIME type
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
But when I execute ,it throws android.content.ActivityNotFoundException exception.So,am I doing anything wrong here?
You are calling getMimeType() and passing it the file name. But your method getMimeType() expects an URL. The documentation for MimeTypeMap.getFileExtensionFromUrl() specifically says:
This method is a convenience method for obtaining the extension of a url and has undefined results for other Strings.
You are probably getting null for the mime type. Add some debug logging and check what getMimeType() is returning.
Also, look in the logcat. It should tell you the content of the Intent it is trying to resolve. That should also give you a hint.
This may help you,
private void readFile(File file){
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");// for .pdf file
//intent.setDataAndType(path, "application/msword");// for msword file
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
String str= "No Application Available to View .pdf file.";
showToast(str);
}
where file is the name of file to be open
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
File file=new File("/sdcard/yourfile");
if(file.exists())
{
Uri path=Uri.fromFile(file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/readername");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(TestActivity.this, "No software for PDF", Toast.LENGTH_SHORT).show();
}
}
}
});