Я новичок в Android, и перед вами стоит проблема, связанная с тем, чтобы открыть PDF-файл ни с чем. В то время как в зефире и других версиях Android он открывается, но в нуге это не так.PDF-файл не открыт в nougat Android
Моего код, как это
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());
}
}
ли кто-нибудь сталкиваться с этой проблемой, как этот
, потому что вы никогда не помещаете файл в свой FileProvider, и вы пропускаете бессмысленный uri – Selvin
@Selvin Как это сделать, можете ли вы поделиться кодом? – Muju
Код в примере из официальной документации андроида. Я не вижу смысла повторять его здесь на SO (особенно, когда это было достаточно долгое время) – Selvin