I am developing on a Android 4.0.3 device. How do I open a file browser for my app? Is there one built in the to Android SDK? Do I have to write my own?
I don't want my app to depend on a the user installing a separate app for file browsing.
To get a file from a file browser, use this:
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
fileintent.setType("gagt/sdf");
try {
startActivityForResult(fileintent, PICKFILE_RESULT_CODE);
} catch (ActivityNotFoundException e) {
Log.e("tag", "No activity can handle picking a file. Showing alternatives.");
}
I'm not quite sure what the gagt/sdf is for... it seems to work in my app for any file.
Then add this method:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Fix no activity available
if (data == null)
return;
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == RESULT_OK) {
String FilePath = data.getData().getPath();
//FilePath is your file as a string
}
}
If the user doesn't have a file manager app installed or preinstalled by their OEM you're going to have to implement your own. You might as well give them a choice.
I hope this one will help you for file picking:
public void performFileSearch() {
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
The code is from this documentation:
https://developer.android.com/guide/topics/providers/document-provider.html
Refer to it for more information.
If someone would still need it for newer versions, it got updated with developing Storage Access Framework by Google for SDK >= 4.4. Check this Google site for further details:
https://developer.android.com/guide/topics/providers/document-provider.html
There is no single file-management app that is installed across all devices.
You probably want your app to be also working on devices with Android 3.x or lower.
The best choice you have though is writing your own file-manager. It isn't as much effort as it might sound, there is a lot of code on this already out there on the web.
Related
I have this code:
protected void pickFile(View view){
///Codigo que abre la galeria de imagenes y carga la imagen en displayedImage
Intent intent = new Intent();
intent.setType("file/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload"), 1);
}
//It's executed when leaving file system
#Override
protected void onActivityResult(int reqCode, int resCode, Intent data){
super.onActivityResult(reqCode, resCode, data);
if (reqCode == 1 && resCode == RESULT_OK && data != null) {
Uri selectedFile = data.getData();
RequestMaker.uploadFile(this, selectedFile, "this is a file");
}
}
What i want to do is to be able to select any file from my phone and send it.
The code works, it opens the chooser and lets me search for any file. However, there are a few problems i am having:
When i try to access via "Internal Storage" option, i cannot select any item. They are all disabled. I fixed that installing a file manager and it lets me choose the files i want, but maybe there is a quick fix for that.
When i select the file and run Uri.getPath(), sometimes the path is valid, others, and in general when i am selecting some image file, there is an error with the path i get in return. Is not the actual one.
I saw some fixes online but they are all for selecting images from the galery, i want the general one.
How can i fix this?
The code works
No, it does not.
First, file/* is not a valid MIME type, or even a wildcard MIME type. There is no MIME type that begins with file/. If you want any MIME type, try */*.
Second, ACTION_GET_CONTENT does not allow the user to "select any file". It allow the user to pick a piece of content, from any app on the device that implements an ACTION_GET_CONTENT activity that elects to honor your MIME type. What is returned by that activity is a Uri pointing to the content. This does not have to be a local file, let alone one that you have direct filesystem access to.
When i select the file and run Uri.getPath(), sometimes the path is valid
No, the path is always valid (at least, for a while). It just is not what you think it is. A Uri is not a file.
For example, presumably you are viewing this Web page in a Web browser. If you look in the address bar of that Web browser, you will see the following URL:
https://stackoverflow.com/questions/33575449/how-to-get-any-type-of-file-with-intent-createchooser-android
By your way of thinking, this is referring to a file, on your hard drive, located at /questions/33575449/how-to-get-any-type-of-file-with-intent-createchooser-android.
That is not the case. Part of the URL indicates a location where the path is relevant; in this case, it refers to a Web server.
A Uri is the same thing. In particular, if the Uri has a scheme other than file:, the Uri is simply an address, one that does not necessarily map to anything you can get to directly. Just as Web browser developers use HTTP to get a stream on the contents of this Web page, so you must use ContentResolver and openInputStream() to get at the contents of content: Uri values.
How can i fix this?
Either:
Use the Uri as a Uri, with openInputStream(), getType(), and similar methods on ContentResolver, or
Do not use ACTION_GET_CONTENT, but instead build your own UI for browsing files that your app happens to be able to reach. This will be a subset of all the files on the device, as not everything is in a location that your app has access to (e.g., files on removable media will be missed). But, it synchronizes your code with your mental model (i.e., that you want files, not content).
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.
I need my app to read text via Camera. I know there's the Tesseract library which does this, but I'd really prefer if there was an app that can handle Intents to read text via Camera, like Xzing does for reading QR codes.
Is there such an app?
There isn't currently an app on Google Play that does this.
I've thought about making one, but the possible use cases for such an app vary much more than for, say, scanning a QR code. There are different possible scenarios:
License plate recognition
Recognition for LCD 7-segment displays
Korean OCR
OCR for stylized text
OCR with shadows or uneven illumination
The different scenarios present a challenge for how to handle the image. A request to such an app via Intent would probably need to specify at least the type of thresholding to use for pre-processing the image along with the language/traineddata file to use.
I've just created an app that takes a photo using the Camera, crop the photo, and return the recognized text as result.
In your app, you may use the following code:
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo("sunbulmh.ocr", PackageManager.GET_ACTIVITIES);
Intent LaunchIntent = pm.getLaunchIntentForPackage("sunbulmh.ocr");
LaunchIntent.setFlags(0);
startActivityForResult(LaunchIntent,5);
} catch (NameNotFoundException e) {
Uri URLURI = Uri.parse("http://play.google.com/store/apps/details?id=sunbulmh.ocr");
Intent intent = new Intent(Intent.ACTION_VIEW,URLURI);
startActivity(intent);
}
Then, get the result in onActivityResult():
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == 5){
String ocr_txt = data.getStringExtra(Intent.EXTRA_TEXT);
// ocr_txt contains the recognized text.
}
}
}
I'm making an Android app which stores some downloaded pdf files inside the device's SD card.
Everything works fine, but now I want to add a function to just pop up the default android file/folder browser showing the directory where my app stores all the PDF (with subdirectories in it) so that the user sees where his documents are stored and can easily browse them.
I've been throught many other SO questions and forum posts, but it seems this can only be done for music/images/contacts/etc. basically those file types which have a 'dedicated browsing system' but not with general file browsing.
I'm actually using this code:
File file = new File("/sdcard/MySorgenia/Documenti/");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
String type = "*/*";
intent.setDataAndType(data, type);
startActivity(intent);
But this will show me a "Choose the application to complete your action" dialog with many applications such as "Music" "Gallery" etc, but no general purpose one.
Thanks!
Because In android there is no any native application which you can use as a File Explorer and responds to Intent type "*/*"
Implement your own File-Explorer Code for this purpose..
Look at these two Links..
openintents
Android-File-Explore
public void loadfile()
{
private static final int gallery=12;
private static final String type="*/*";
Intent i=new Intent(Intent.ACTION_GET_CONTENT);
i.setType(type);
startActivityForResult(Intent.createChooser(i,"select file"), gallery);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == gallery && resultCode == RESULT_OK && data != null) {
Uri uploadfileuri = data.getData();
File file = new File(uploadfileuri.getPath());
}
}
Since Android 4.4 KitKat (API level 19), there is an Android built-in file picker: your app invokes the ACTION_OPEN_DOCUMENT and/or ACTION_CREATE_DOCUMENT intent and receives the files returned by document providers. More info about that can be found here:
Open files using storage access framework | Android Developers
Depending on where you want to store files, you may need to request permission:
Request App Permissions | Android Developers
Here is a how to:
An Android Storage Access Framework Example - Techtopia.
And a great working example is Ian Lake's Local Storage. Its source can be found on GitHub:
https://github.com/ianhanniballake/LocalStorage
And the app can be downloaded from Google Play:
https://play.google.com/store/apps/details?id=com.ianhanniballake.localstorage
Most android distributions do not come with a default file browser, and the behavior you noticed is the default android behavior. If there's any good third party file browser installed, it will automatically show up in that list. However it is not guaranteed that every end user will have a file browser installed. A general purpose fragment-widget can be created for this (and probably shared with others).
Look at this file picker, it's the best one I found:
I saw a way of reading online pdf files using google docs ...
Android - Load PDF / PDF Viewer
Is there a way we can use it to view local files stored in sd card
You can launch an intent that will allow the user to choose what app will open the PDF with the following code, which will work for any file and mimetype. If the user doesn't have an app that can open it, you can display an error or do whatever else you need to do.
Note that the file must be world-readable, so it must be marked as such if it is on Internal storage, or it must be in external storage.
private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
startActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}