How to open directory by default app android - android

Please help me about this question.
I know how to open a file by default app like this :
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = ExternalStorageHelper.getFileFromName(fileName);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext =fileName.substring(fileName.lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
if(type != null){
if(mime.hasMimeType(type)){
intent.setDataAndType(Uri.fromFile(file),type);
containActivity.startActivityForResult(intent,1);
}else{
Toast t = Toast.makeText(containActivity, R.string.download_open_file_error_message, Toast.LENGTH_SHORT);
t.show();
}
}else{
Toast t = Toast.makeText(containActivity, R.string.download_open_file_error_message, Toast.LENGTH_SHORT);
t.show();
}
but I can't find out how to open a folder by default app.
From this code how can I determine to MimeTypeMap fileName is a Directory.
I am really need it now, please help me :)

Unfortunately, there is no built in way to do that. You can try using
the OI FileManager - it is quite nice but the user is required to
install the app to use it.
There's also a good example of how to make your own here.
How do I open the file browser? ( Android SDK)

Related

Error:Error: try is not a valid resource name (reserved Java keyword)

I want to display a pdf file in my app . Where i was stored pdf file in raw folder . Where pdf name is "try.pdf" my code is this :
File file = new File(R.raw.try);
Uri path = Uri.fromFile(file);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path , "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent );
} catch (ActivityNotFoundException e) {
Toast.makeText(this,"No Application available to viewPDF", Toast.LENGTH_SHORT).show();
}
But this code shows an error Error:Error: try is not a valid resource name
(reserved Java keyword)
Can anyone help me.
Here is the error
File file = new File(R.raw.try);
Rename your file try.pdf to anything else (except whats mentioned here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html). You can't use a Java-reserved keyword, which try is.
Important point: You can not use a reserved keywords.

Android open file and let user choose which program can open it

I'm working on a program which needs to show list file in ListView. I got this list with path_of_files. But this is what i want to ask: when i clicked on an item, how can user open it? I think i want a popup let user choose which program user want to open it.
For example: I have a path: /sdcard/file.xls. When user clicked in, a popup shows some application like: Microsoft Excel, Google Spreadsheet (I only want to work with xls - and it only works with it, still ok). How can i do it?
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "directory");
for (File f : yourDir.listFiles()) {
if (f.isFile()) {
String name = f.getName();
Long lastModified = f.lastModified();
if (name.toLowerCase().contains(".xls"))
fileObjList.add(new FileObj(0,name,lastModified));
}
}
This is my code to get filename and file path.
First , set a listener to your listView Items . And inside the handler callback you can do something likebelow to open up excels :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(context, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}

Android, can't view file stored in external memory through intent

I have a function that downloads and extracts a zip file.
Its extracted to the external storage in: ../Android/data/packagename/..
When I try the following intent to view a .mp4 video for example, its opened in a video player and says that it can't open the file. (not only .mp4 files)
Uri uri = Uri.parse(Helper.getStorageDir(getActivity()) + "/" + mediaObject.getLinkOffline());
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(mediaObject.getLinkOffline().substring(mediaObject.getLinkOffline().lastIndexOf(".") + 1, mediaObject.getLinkOffline().length()));
newIntent.setDataAndType(uri, mimeType);
newIntent.setFlags(newIntent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No handler for this type of file.", 4000).show();
}
However, when I go through a file explorer and open the same file there is no problem.
I have there read/write permissions in my manifest. But thats obviously not the problem since I can read write the file and can also check the the file exists. It just wont let an external app open the file through an intent from my app.
Am I missing something?
EDIT: when I debug and check the mime type for the mp4 file it is "video/mp4".

Android app using Monodroid unable to get external program to correctly open file

I'm trying to get external applications to open different file types determined at runtime. From everything I've looked at I feel I'm doing it right but none of the programs open the files correctly. For Astro using text files it shows this error: java.lang.illegalargumentexception invalid uri used for observer. Basically it appears not to have gotten the right file location. Any help would be greatly appreciated.
Here is my code:
System.Net.WebClient webClient = new System.Net.WebClient();
String attachmentPath = appManager.getAttachmentPath(plexState.getActiveBrain().getGuid(), attachment.getId(), attachment.getType());
webClient.DownloadDataCompleted += (object sender, System.Net.DownloadDataCompletedEventArgs eventArgs) =>
{
notificationManager.hidePopup();
if ((eventArgs.Cancelled == false) && (eventArgs.Error == null))
{
byte[] fileBytes = eventArgs.Result;
Java.IO.File attachmentFile = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads), attachment.getName());
System.IO.File.WriteAllBytes(attachmentFile.AbsolutePath, fileBytes);
// Get MIME type
String extension = MimeTypeMap.GetFileExtensionFromUrl(attachmentFile.AbsolutePath);
String mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
Intent intent = new Intent(Intent.ActionView);
Logger.console("File exists: " + attachmentFile.Exists()); // Shows true.
intent.SetData(Android.Net.Uri.FromFile(attachmentFile));
intent.SetType(mimeType);
// Check if any application can open the given MIME type. Otherwise show notice to user
PackageManager packageManager = Activity.PackageManager;
IList<ResolveInfo> resolversList = packageManager.QueryIntentActivities(intent, 0);
if (resolversList.Count > 0)
{
Activity.StartActivity(intent);
}
else
{
Toast.MakeText(Activity, "Unable to find for ext...", ToastLength.Long);
}
}
else if (eventArgs.Error != null)
{
Logger.console(eventArgs.Error.Message);
}
};
webClient.DownloadDataAsync(new Uri(attachmentPath));
I just found the answer to this issue in case anyone else runs into it. I'm not sure if this is just a Monodroid bug or there is something else at play that I'm unaware of. To solve this issue take these two lines of code:
intent.SetData(Android.Net.Uri.FromFile(attachmentFile));
intent.SetType(mimeType);
and condense to this:
intent.SetDataAndType(Android.Net.Uri.FromFile(attachmentFile), mimeType);
I can't believe this worked. After spending almost two days on this I thought just to condense the code and could not at first figure out why it started working.

get file path of sharedpreferences file

I am creating a sharedpreferences file in my android code. I then want to email that file in my code. For that I need to access the path of the sharedpreferences file. The code I am using is below. But it does not seem to work. I am able to open the email but there is no attachment there since I guess it could not get the file. Can someone suggest me any solution here.
File f = getDatabasePath("userPrefsFile.xml");
String filelocation=f.getAbsolutePath();
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("application/xml");
String[] to = {"test#test.com"};
email.putExtra(Intent.EXTRA_EMAIL, to);
email.putExtra(Intent.EXTRA_STREAM,filelocation);
email.putExtra(Intent.EXTRA_SUBJECT,"test file send");
startActivity(Intent.createChooser(email, "Send email"));
So SharedPreferences files are located at directory
/data/data/your.package/shared_prefs
So you need to use path above.
Pseudo-code:
File root = new File("/data/data/your.package/shared_prefs");
if (root.isDirectory()) {
for (File child: root.listFiles()) {
Toast.makeText(this, child.getPath(), Toast.LENGTH_SHORT).show();
}
}
Reason why you cannot use getDatabasePath() is that returns databases folder 1
/data/data/your.package/databases/
1 Same issue is also related to getFileStreamPath() method that returns
/data/data/your.package/files

Categories

Resources