I have a pdf opened with my own application. Now when user click on a button I am showing openwith options of application/pdf. Now the user select his choice (for example adobe reader) and the opened pdf file must be shown in user choice (adobereader in this case). I have bytearray and input stream with me for the opened PDF.
Try this
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
}
EDIT 1
OutputStream out = new FileOutputStream("out.pdf");
out.write(bArray);
out.close();
After creating 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
}
EDIT 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
}
this may helps you
EDIT 3
The following code was tested by myself and it's working as u want
Create the pdf file:
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");
}
}
Open the pdf file:
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
add the following permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Note :
1.Change the code order as you want.
2.call createPDF() and then OpenPDF().
This is working code.
when clicked on openwith in app.. im using content provider as my file is not available physically...
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.