英文:
opening pdf file in external storage in default pdf viewer app on a button click
问题
以下是您提供的代码的翻译部分:
在按钮点击下面,以下函数被执行,当提示时我授予了存储权限。如果没有StartActivity行,它就不会崩溃,有人可以帮我吗?我想要用设备默认的PDF查看器打开一个特定的现有文件。权限、文件存在或PDF查看器应用程序的存在都没有错误。
public void openfile(View v) {
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
} else {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "notes.pdf"); // -> 文件名 = notes.pdf
if (pdfFile.exists()) {
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "没有可用于查看PDF的应用程序", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(NewActivity.this, "不存在此文件", Toast.LENGTH_SHORT).show();
}
}
}
请注意,上述翻译仅包括代码部分,不包括任何其他内容。
英文:
on a button click below function is executed , I gave it storage permission when prompted to. without the StartActivity line it's not crashing , can anyone help me with this , i want to open a specific existing file with device default pdf viewer. ther is no error with permissions or file existance or pdf viwer application presence.
public void openfile(View v) {
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
} else {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "notes.pdf"); // -> filename = maven.pdf
if (pdfFile.exists()) {
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(NewActivity.this, "No such file exists", Toast.LENGTH_SHORT).show();
}
}
}
I tried a lot of times , it didn't even shown a "no application found to view pdf " error
straightaway its crashing
答案1
得分: 1
I got the answer , now its working , Had to use FileProvider , now it's more like
if anyone facing same problem let me know I will post step by step solution
public void openfile(View v) {
if (!checkstoragepermission()) {
requestReadpermission();
}else {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "/materials/"+"notes.pdf"); // -> filename = maven.pdf
if (pdfFile.exists()) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + "Zio app" ,"materials");
File newFile = new File(imagePath, "notes.pdf");
Uri path = FileProvider.getUriForFile(this, "com.example.zio.fileprovider", newFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = Intent.createChooser(pdfIntent, "Open File");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(NewActivity.this, "No such file exists", Toast.LENGTH_SHORT).show();
}
}
}
英文:
finally I got the answer , now its working , Had to use FileProvider , now it's more like
if anyone facing same problem let me know I will post step by step solution
public void openfile(View v) {
if (!checkstoragepermission()) {
requestReadpermission();`
}else {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio
app/" + "/materials/"+"notes.pdf"); // -> filename = maven.pdf
if (pdfFile.exists()) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/"
+ "Zio app" ,"materials");
File newFile = new File(imagePath, "notes.pdf");
Uri path = FileProvider.getUriForFile(this,
"com.example.zio.fileprovider", newFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = Intent.createChooser(pdfIntent, "Open File");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "No Application available to
view PDF", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(NewActivity.this, "No such file exists",
Toast.LENGTH_SHORT).show();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论