How to Set file permissions- Android - android

I have a doc file on sdcard.
I want to open it in read-only mode. Also I don't want the document viewer to save the file.
The intent to open the file is as follows:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.doc");
file.setReadOnly();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION );
intent.setDataAndType(Uri.fromFile(file), "application/msword");
startActivity(intent);
But when the file opens in quickOffice, the file is still editable.
Please suggest how should I make the file ready-only to any document viewer and how should I prevent file from saving.

It is probably because your app does not have root access. File permissions can be changed by root access only.

Related

Is there a way to open a file stored in andorid assets with a specific application?

I'm trying to open a file from assets with an application using the code below:
String fileUri = "file:///android_asset/extensions/file.novabackup";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fileUri), "application/octet-stream");
intent.setPackage("com.teslacoilsw.launcher");
startActivity(intent);
However, it does nothing and simply returns me to the MainActivity. I've also granted READ and WRITE access.

Cannot open word file in edit mode in android using intent

Here is the intent I use to open a word file:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(fileUri, MimeTypeMap.getSingleton().getFileExtensionFromUrl(fileUri));
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
mContext.startActivity(intent);
I feth the fileUri using this line:
FileProvider.getUriForFile(context, "my authority", file);
File is opened correctly in both Microsoft word app and OfficeSuite app, but in both apps, the file is in read only mode and I cannot edit the file directly. When I open that word file with a file manager app like ES File Explorer, the file is in read write mode and edit is enabled.
I tried both ACTION_VIEW and ACTION_EDIT and the same happened.

Show local PDF-File with external app

I would like to open a local PDF file with a pre-installed PDF-viewer via Share-Intent like this:
Uri uri= Uri.parse("file:///android_asset/manual.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType("application/pdf");
startActivity(intent);
The app-picker is shown, when I click on Adobe PDF it doesn't show the PDF-file however.
Am I doing something wrong, or is it simply not possible?
This worked for me,
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/directory/"+theNamesOfFiles)),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
You have 2 options:
Put the file in a public storage where the PDF viewer app can access it
Use a ContentProvider class to give that PDF viewer access to a private file.
See:
How to copy files from 'assets' folder to sdcard?
http://developer.android.com/guide/topics/providers/content-provider-creating.html

How to giving address from assets folder?

I intend opening a PDF file with the default application of my device. I have a problem giving address from the assets folder.
My giving address code is as follows:
File pdfFile = new File("file:///android_assets/test.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
This code gives the error This path is not valid when Adobe Reader opens. What's the correct address?
It shows the error because only your application have the permission to access that folder. You can think to copy the file in a different directory (e.g. some place in the sdcard) and provide acrobat reader with the uri to the copied file,

android how to make accessible data folder content to other application

I want to open a pdf file saved in application data folder using pdf viewer application. How can i make accessible data folder content to other application. Some code snippet will help me lot.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType( Uri.parse("content://" + pdf_path), "application/pdf" );
startActivity(intent);
File file = new File(“/sdcard/read.pdf”);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
You can access pdf file from external memory by this.make sure you are giving correct path of it.
First of all make sure your .pdf file has, MODE_WORLD_READABLE Permission like,
Now to access .pdf file from application data folder means, /data/data/<package_Name>/
Use, getFilesDir() which returns the path of Application data directory,
Then,
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(getFilesDir()+ "pdf_path")),"application/pdf" );
startActivity(intent);

Categories

Resources