无法在应用程序中打开.pdf文件 [英] Unable to open .pdf file in the application
问题描述
我试图显示在我的应用程序一个PDF文档。我无法显示。我收到无效的文件路径误差Adobe Reader和PDF阅读器显示文件无法打开。请你的任何一个让我知道了什么错误,我在这里干什么。如果有更好的方法实现这一请教我。我已经发布了,我现在用的是code:
I am try to display a .pdf document in my application. I am unable to display it. I am getting invalid document path error with adobe reader and pdf viewer display file cannot be opened. Please any one of you let me know what mistake am I doing here. If there is a better way in achieving this please teach me. I have posted the code which I am using:
public class HelpScreen extends ActionBarActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_screen);
// Initializing, setting text and color of tool bar
toolbar = (Toolbar) findViewById(R.id.appBar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
CopyReadAssets();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_help_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
Log.d("Pana", "The value of assests is " +assetManager);
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "help_document_task_management_system_document_4.pdf");
try
{
in = assetManager.open("help_document_task_management_system_document_4.pdf");
out = openFileOutput(file.getName(), Context.MODE_PRIVATE);
copyFile(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() + "/" + "/help_document_task_management_system_document_4.pdf"),
"application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
}
EDITED code:
EDITED CODE:
public class HelpScreen extends ActionBarActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_screen);
// Initializing, setting text and color of tool bar
toolbar = (Toolbar) findViewById(R.id.appBar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
CopyReadAssets();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_help_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void CopyReadAssets() {
AssetManager assetManager = getAssets();
Log.d("Pana", "The value of assests is " + assetManager);
InputStream in = null;
OutputStream out = null;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf");
// File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf");
try {
in = assetManager.open("help_document_task_management_system_document_4.pdf");
out = openFileOutput(file.getName(), Context.MODE_PRIVATE);
copyFile(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.fromFile(file), "application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
请让我知道我的错误,并帮助我走出了这一点。先谢谢了。
Please let me know my mistake and help me come out of this. Thanks in advance.
推荐答案
第三方应用程序没有权限访问该文件。使用 FileProvider
来为它服务,或将文件复制到的外部存储代替的内部存储。
Third party apps have no rights to access that file. Use FileProvider
to serve it, or copy the file to external storage instead of internal storage.
这篇关于无法在应用程序中打开.pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!