Вы не можете открыть pdf-файл непосредственно из папки с ресурсами. Сначала вам нужно записать файл на SD-карту из папки с ресурсами, а затем прочитать его с SD-карты.
Попробуйте следующий код, чтобы скопировать и прочитать файл из папки активов:
//method to write the PDFs file to sd card
private void PDFFileCopyandReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "test.pdf");
try
{
in = assetManager.open("test.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
readFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/test.pdf"),
"application/pdf");
startActivity(intent);
}
private void readFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
Открыть файл из SDCard, как показано ниже:
File file = new File("/sdcard/test.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
обеспечивают также разрешение на запись в ваш внешний хранения в вашем манифесте.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
проверить это [ссылка] [1] ссылка может помочь вам [1]: http://stackoverflow.com/questions/11820142/how-to-pass-a-file-path-which -is-in-assets-folder-to-filestring-path – Rama
привет, просто посмотри ответ, который я написал, надеюсь, что это поможет тебе! –
Проверьте мой ответ. @String – GrIsHu