我尝试在我的应用程序中显示.pdf文档。我无法显示它。我无法使用adobe reader获取无效的文档路径错误,无法打开pdf viewer显示文件。请你们中的任何一个让我知道我在这里做的错误。如果有更好的方法来实现这一点,请教我。我已经发布了我正在使用的代码:
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);
}
}
}
编辑代码:
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);
}
}
}
请让我知道我的错误并帮助我解决这个问题。提前谢谢。
答案 0 :(得分:2)
第三方应用无权访问该文件。使用FileProvider
投放,或将文件复制到external storage而不是internal storage。
答案 1 :(得分:0)
在您的意图创建中/
之后,您的文件路径中似乎有两个getFilesDir()
。