Android default file browser intent showing only *.json files - android

My goal is to list only *.json files in android filebrowser, to load them to the software.
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + PATH_TO_WRITE);
Uri uri = Uri.fromFile(folder);
intent.setDataAndType(uri, "file/.json");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Activity activity = (Activity) context;
activity.startActivityForResult(intent, FILECHOOSER_RESULTCODE);
Looks like the Android filebrowser dosen't support *.json files. Is there way to implement FileFilter to the intent?

Have you tried using
intent.setDataAndType(uri, "application/json");
Edit:
It appears although this currently isn't supported in the default android file explorer (Please see this)

Sovled this by using custom made file browser because android does not support *.JSON formats. Used this library https://github.com/adityak368/Android-FileBrowser-FilePicker , which was easy to implement

Related

How can I pass a PDF to Adobe Reader on Android by intent to edit ist directly and not a copy?

I use the following code to open a pdf form. If I use Acrobat Reader it's not possible to write back to the original file. It always makes a copy which I only could guess but newer know for shure in my app.
I know that it has to be possible to edit the original because if I open a pdf from any file manager (e.g. the one from Asus) Adobe Reader edits it directly.
Xodo PDF allows direct edit. It even supports the correct ACTION_EDIT intent but I'm afraid that our users insist to use Adobe...
How can I edit the original with Adobe?
final File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + File.separator + "x.pdf");
if(file.exists())
{
file.setWritable(true,false);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = FileProvider.getUriForFile(Objects.requireNonNull(getContext()),
BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setDataAndType(contentUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent = Intent.createChooser(intent, "Open File");
startActivity(intent);
}
At the moment even Xodo doesn't work anymore but I was lucky to realized that OneDrive-PDF-Viewer does.

Sharing an Audio wav file

I want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.

Android Studio - Open epub + pdf files from default directory application (Programmatically)

How to open default file manager app that let you choose a file but only with extension .epub or .pdf programatically via Intent?
The code shown is the declaration of the intent that you have to start for opening the default file manager app that let you choose a file but only with extension .epub or .pdf, I hope it will be useful.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/epub+zip");
String[] mimetypes = {"application/epub+zip", "application/pdf"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent,PICKFILE_RESULT_CODE);
It is a mix of solutions I found that work so maybe it is redundant. Let me know what you think!

E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist

I'm trying to open a pdf after creating it via an intent. The file exists and is readable but save inside the apps directory.
Currently the file is saved in the following manner
OutputStream out;
try {
//TODO: expose through a content provider
out = mContext.openFileOutput(outputFileName, Context.MODE_WORLD_READABLE);
document.writeTo(out);
out.close();
and the file is sent to an intent with the following code
final Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
mActivity.startActivity(viewIntent);
The app trying to open it is the google drive viewer and I see in the Android monitor
E/PdfViewerActivity: fetchFile:file: java.io.FileNotFoundException: file does not exist
This previously worked and I don't think I've changed anything related to this code however I have updated my tooling. I've tried changing the buildToolsVersion in the build.gradle back to what it was before as well as the support libraries 'com.android.support:appcompat-v7:22.+' and 'com.android.support:design:22.+' instead of version 24.2.1
I've tried using the debugger to set the file to be readable just before sending the intent but that did not work.
Thanks
I found the answer starting with the Android 7 Behavior Changes documentation. Sharing files is no longer allowable when setting MODE_WORLD_READABLE even though the documentation doesn't mention any of this
I followed the documentation on sharing files to fix the issue but the sample code in the documentation was more complicated than what I needed. I still had to add the file provider to the AndroidManifest.xml as well as create the fileprovider.xml.
The resulting code is now
final Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(mActivity, "my.authority.fileprovider", file);
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewIntent.setDataAndType(fileUri, "application/pdf");

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.

Categories

Resources