[英]open pdf with openwith option android
我用自己的应用程序打开了pdf。 现在当用户点击按钮时,我显示openwith选项application / pdf。 现在用户选择他的选择(例如adobe reader),打开的pdf文件必须显示在用户选择中(在这种情况下为adobereader)。 对于打开的PDF,我有bytearray和输入流。
试试这个
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
编辑1
OutputStream out = new FileOutputStream("out.pdf");
out.write(bArray);
out.close();
创建pdf后,
File file = new File("filepath");
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
编辑2
File myFile = new File("out.pdf");
OutputStream out = new FileOutputStream(myFile);
out.write(bytArray);
out.close();
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(myFile),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
这可能对你有所帮助
编辑3
以下代码由我自己测试,它可以按照您的需要运行
创建pdf文件:
File resolveMeSDCard = new File("/sdcard/download/media/output.pdf");
public void createPDF()
{
byte[] byt = new byte[]{1,2,3,4,5};
File mediaDir = new File("/sdcard/download/media");
if (!mediaDir.exists()){
mediaDir.mkdir();
}
FileOutputStream fos;
try {
//File resolveMeSDCard = new File("/sdcard/download/media/output.pdf");
resolveMeSDCard.createNewFile();
fos = new FileOutputStream(resolveMeSDCard);
fos.write(byt);
fos.close();
System.out.println("Your file has been written");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Your file has not been written");
}
}
打开pdf文件:
public void openPDF()
{
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(resolveMeSDCard),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
}
的manifest.xml
添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Note :
1.根据需要更改代码顺序。
2.call createPDF()然后打开OpenPDF()。
这是工作代码。
当在应用程序中点击openwith时...我正在使用内容提供商,因为我的文件在物理上不可用...
try {
createCachedFile(this, attachmentFileName, bytes);
startActivity(pdfOpenWith(this, attachmentFileName, bytes));
}catch (IOException e) {
e.printStackTrace();
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Not available on this device.", Toast.LENGTH_SHORT).show();
}
public void createCachedFile(Context context, String fileName, byte[] content) throws IOException {
File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
cacheFile.createNewFile();
BufferedOutputStream bostream = new BufferedOutputStream(new FileOutputStream(cacheFile));
bostream.write(content);
bostream.flush();
bostream.close();
}
public static Intent pdfOpenWith(Activity context, String fileName, byte[] bytecontent)
{
Intent pdfintent = new Intent(Intent.ACTION_VIEW);
//pdfintent.setType("application/pdf");
Uri contenturi = Uri.parse("content://" + AttachFileProvider.AUTHORITY + "/" + fileName);
Toast.makeText(context, fileName, Toast.LENGTH_LONG).show ();
pdfintent.setDataAndType(contenturi, "application/pdf");
pdfintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return pdfintent;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.