I'm trying to open a file downloaded using DownloadManager. I'm getting the local filename after download is finished. After that I suggest my user to open file (or decline). The method I use for opening file is:
private void openFile(String fileName) {
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl((fileName));
String type = map.getMimeTypeFromExtension(ext);
Intent install = new Intent(Intent.ACTION_VIEW);
Log.d(TAG, "openFile Trying to open file: " + Uri.fromFile(new File(fileName)));
install.setDataAndType(Uri.fromFile(new File(fileName)), type);
try {
mCx.startActivity(install);
} catch (Exception e) {
//Si no hay ninguna app capaz de abrir el archivo, fallará.
e.printStackTrace();
Toast.makeText(mCx, mCx.getString(R.string.sin_app_archivo), Toast.LENGTH_LONG).show();
}
}
The filename I pass to that method is received from DownloadManager query method:
@Override
public void onReceive(Context context, Intent intent) {
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (myDownloadreference == reference) {
Log.d(TAG, "onReceive descarga es la nuestra");
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(reference);
Cursor cursor = mDownloadManager.query(query);
cursor.moveToFirst();
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
//This is String I pass to openFile method
String savedFilePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
...
}
}
It usually works with most of filetype. If there isn't any app able to open it, it will show a Toast
.
The strange behaviour happens with files with spaces on its name. It always shows the Toast (even if apps for that mime type are installed) when I execute my openFile method. Nevertheless, by clicking on DownloadManager notification, Android open PDF file (or shows App picker if two or more Apps can handle file opening). So, what is the Intent that SO launches when user clicks on notification? How can I change my method in order to open files with spaces on its name also?
An example of file that can be openned with openFile method:
D/GdDocumentacionDownloaderHelper﹕ openFile Trying to open file: file:///storage/emulated/0/Android/data/es.ineco.app/files/Download/ic_action_delete-11.zip
And another example of file that can't be openned with openFile method:
D/GdDocumentacionDownloaderHelper﹕ openFile Trying to open file: file:///storage/emulated/0/Android/data/es.ineco.app/files/Download/10-02_Prevbat_363%2B570_363%2B820_V1%20(1)-5.pdf
EDIT
After @CommonsWare advices, It is working now. Problem was with received param I was using from DownloadManager. Correct file name to use in my openFile()
method is
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Instead of:
cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
foo.pdf
. – TelespectroscopefileName
coming from? – TelespectroscopeCOLUMN_LOCAL_URI
instead, to avoid extra converting on your side, and see if that helps. – Telespectroscope