Is there any possible way to open a .doc extension file?
Here is the complete way to open .doc file in Android 7.0 or less:
Step-1: First of all, place your pdf file in assets folder like the following screenshot.
Step-2: Now go to build.gradle file and add following lines:
repositories {
maven {
url "https://s3.amazonaws.com/repo.commonsware.com"
}
}
and then under dependencies add the following line and sync:
compile 'com.commonsware.cwac:provider:0.4.3'
Step-3:
Now add a new java file which should extend from FileProvider Like in my case file name is LegacyCompatFileProvider and code inside of it.
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.FileProvider;
import com.commonsware.cwac.provider.LegacyCompatCursorWrapper;
public class LegacyCompatFileProvider extends FileProvider {
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder)));
}
}
Step-4:
Create a folder named "xml" under "res" folder. (If the folder is already there then no need to create). Now add a providers_path.xml file, in xml folder. Here is the screenshot:
Inside file add following lines:
Step-5:
Now go to AndroidManifest.xml file and following lines in tag:
Step-6: Now go to the Activity class from where you want to load pdf and add following 1 line and these 2 methods:
private static final String AUTHORITY="REPLACE_IT_WITH_PACKAGE_NAME";
static private void copy(InputStream in, File dst) throws IOException {
FileOutputStream out=new FileOutputStream(dst);
byte[] buf=new byte[1024];
int len;
while ((len=in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
private void LoadPdfFile(String fileName){
File f = new File(getFilesDir(), fileName + ".doc");
if (!f.exists()) {
AssetManager assets=getAssets();
try {
copy(assets.open(fileName + ".doc"), f);
}
catch (IOException e) {
Log.e("FileProvider", "Exception copying from assets", e);
}
}
Intent i=
new Intent(Intent.ACTION_VIEW,
FileProvider.getUriForFile(this, AUTHORITY, f));
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
finish();
}
Now call LoadPdfFile method and pass your file name without .doc like in my case "chapter-0" and it will open doc file in doc reader application.