I have a word document(.doc or .docx) in the server. I need to open in my android application by clicking one button .doc file have to open. How to do this? anyone, please help me.
I tried this code :
File file = new File(Environment.getExternalStorageDirectory(),
"Document.docx");
try {
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
// replace "application/msword" with
// "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
// for docx files
// objIntent.setDataAndType(path,"application/msword");
objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);
} else {
Toast.makeText(PMComplitionProjectActivity.this, "File NotFound",
Toast.LENGTH_SHORT).show();
}
} catch (ActivityNotFoundException e) {
Toast.makeText(PMComplitionProjectActivity.this,
"No Viewer Application Found", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
e.printStackTrace();
}
I have this link that shows how to open any file in Android : http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html
Also, there is a similar case, which is done by PDF file : Android : how to open pdf file from server in android
I hope these links will work for you.
Good Luck,
G.
Related
I want to delete some images from my phone. I have image path and I simply do this
try {
File file = new File(content);
if (file.canRead()) {
boolean mDeleleFile = file.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(content))));
if (mDeleleFile) {
Toast.makeText(getApplicationContext(), "File deleted", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Even if I get File deleted when I open gallery image still exist! Why is that? What I'm missing?
your may have created a new file after deleting it.
In my app i've got some data which I'd like to display into an Excel sheet.
Some days ago I've already managed to build the file .xls.
I've also been able to sent it by email whith this code:
FileOutputStream fos = null;
try {
file = new File(getContext().getFilesDir(), (main.getNomeAzienda() + "" + getDate(System.currentTimeMillis(), "dd-MM-yyyy_HH-mm")) + ".xls");
fos = new FileOutputStream(file);
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{FirebaseAuth.getInstance().getCurrentUser().getEmail()});
i.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), "com.example.authority.fileprovider", file));
i.putExtra(Intent.EXTRA_SUBJECT, "Raccolto globale aSista");
i.putExtra(Intent.EXTRA_TEXT, "In allegato il File Excel con i dati filtrati.");
try {
startActivity(Intent.createChooser(i, "Seleziona il Client di posta che vuoi utilizzare..."));
} catch (android.content.ActivityNotFoundException ex) {
Snackbar.make(main.getFab(), "Non ci sono client di posta disponibili installati sul dispositivo.", Snackbar.LENGTH_SHORT).show();
}
}
Snackbar.make(main.getFab(), "Foglio Excel generato.", Snackbar.LENGTH_SHORT).show();
Sorry if some parts of the code are in italian.
Anyway, now I'd like to display the excel file directly on the phone.
I downloaded some apps which should be able to display .xls file. Like Excel, this one from google and polaris office.
With this code i call the intent which ask the user to choose one of those app and open the file:
Uri path = Uri.fromFile(finalFile);
Intent excelIntent = new Intent(Intent.ACTION_VIEW);
excelIntent.setDataAndType(path , "application/vnd.ms-excel");
excelIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(excelIntent);
}
catch (ActivityNotFoundException e) {
Snackbar.make(main.getFab(),"Impossibile aprire il file su questo dispositivo",Snackbar.LENGTH_LONG).show();
}
I've faced some problems related to the name of the file, which should't include some special characters. Then i finally been able to start opening the file but now i've got a problem.
Excel tells me "is not possible to open the file, an error has occurred".
Google docs the same.
Polaris doesn't display errors but it show an empty file.
Your file is in getFilesDir(). That is part of internal storage, and third-party apps do not have access to your portion of internal storage.
Since you already have FileProvider set up, use the FileProvider Uri, and use Intent.FLAG_GRANT_READ_URI_PERMISSION to give temporary read access to the other app.
I am having an issue, I have never had problem opening files via ACTION_VIEW the next way:
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
String dataType = "image/*";
if (file.exists()) {
Intent fileIntent = new Intent(Intent.ACTION_VIEW);
fileIntent.setDataAndType(Uri.fromFile(file), dataType);
fileIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(fileIntent, "Open file");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "There is a problem when opening the file");
}
} else {
Toast.makeText(getContext(), "Invalido", Toast.LENGTH_LONG).show();
}
The problem I am having right now is that even though the file exists when I choose the app to open the file it immediately closes and tells me Not found. I have put the image I am loading in an image view and there is no problem, so the file is valid but for some reason it has conflicts when I am opening it via intent.
I am aware that it may have something to do with the way I am creating the file, I am retrieving it from Google drive so I am writing the file using the Apache Commons library the next way:
DriveContents contents = result.getDriveContents();
InputStream inputStream = contents.getInputStream();
File file = new File(getActivity().getFilesDir(), TEMP_FILE_NAME);
try {
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
What is it I am doing wrong? I am not totally sure if the problem has to do with the copy method executing asynchronously or something like that.
Thanks in advance.
I have never had problem opening files via ACTION_VIEW the next way
That code will never work, as third-party apps have no rights to work with files on getFilesDir() of your app.
What is it I am doing wrong?
You are attempting to serve an inaccessible file to third-party programs. Use FileProvider to serve the file, using FileProvider.getUriForFile() to get the Uri to use in your ACTION_VIEW Intent.
I am downloading a pdf file from dropbox, it is downloaded in my sdcard, how can i read it, i know many people have asked this question, but i have not found a good answer uptil now, any help or guidance would be appriciated
Try This:
Uri uri = Uri.fromFile(mActivity.getFileStreamPath(mFileName));
try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(uri, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
I want to be able to open a pdf file from my application without any other application like pdf viewer. I don't want to make the user install an other appliation to open pdf files.
Is there any open source library that I can use in my project?
You can open a PDF in a webview using google docs. The URL format is
https://docs.google.com/gview?embedded=true&url=http://link.to.your/pdf_file.pdf
You can use, for instance, MuPdf Reader. I described how to build it here.
You can open a PDF in a webview using google docs. The URL format is
https://docs.google.com/gview?embedded=true&url=http://link.to.your/yourfile.pdf
or you can open pdf using pdf viewer Follow this code
FileFinalpath = SdCardpath + "/" + Filepath + Filename;
File file = new File(FileFinalpath);
if (file.exists()) {
Uri filepath = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(filepath, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (Exception e) {
alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);
Log.e("error", "" + e);
}
} else {
alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);
}