我是android新手,我在Android Nougat设备上打开pdf文件时遇到问题。我可以在棉花糖上打开pdf但不能在牛轧糖中打开
我的代码是这样的,
//regionOpen pdf file of Employee Induction with permission
private void CopyReadAssets()
{
AssetManager assetManager = getResources().getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "Induction.pdf");
try
{
copyFile(assetManager.open("Induction.pdf"), file);
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.setDataAndType(getUri(file), type);
Uri uri = Uri.parse("content://com.econnect.team.PdfContentProvider/"+"Induction.pdf");
// Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".PdfContentProvider", file);
intent.setDataAndType(uri, type);
} else {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(Uri.fromFile(file), type);
Log.e("Uri ", " "+ Uri.fromFile(file));
}
startActivityForResult(intent, 100);
}catch (FileUriExposedException ex){
Log.e("Uri ", "exception: "+ex.getMessage());
}
catch (ActivityNotFoundException anfe) {
Toast.makeText(Menus.this, "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
}
private void copyFile(InputStream in, File f) throws IOException
{
try {
FileOutputStream out = new FileOutputStream(f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
out = openFileOutput(f.getName(), Context.MODE_PRIVATE);
}
else {
out = openFileOutput(f.getName(), Context.MODE_WORLD_READABLE);
}
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.close();
}catch (Exception e){
Log.e("copy file", e.getMessage());
}
是否有人面临同样的问题,如果解决了这个问题,请帮助我。 提前谢谢。