2016-07-04 4 views
4

Ich habe ein pdf mit meiner eigenen Anwendung geöffnet. Jetzt, wenn Benutzer auf einen Knopf klickt, zeige ich openwith Optionen von Anwendung/pdf an. Jetzt wählt der Benutzer seine Wahl (zum Beispiel Adobe Reader) und die geöffnete PDF-Datei muss in der Benutzerauswahl angezeigt werden (in diesem Fall adobereader). Ich habe Bytearray und Eingabestream mit mir für das geöffnete PDF.open pdf mit openwith option android

+0

Und was ist Ihre Frage? – AxelH

+0

hallo, es wird standardmäßig angezeigt. aber was hast du bisher probiert? –

+0

openwith Fenster wird standardmäßig angezeigt, aber wenn ich auf eine Schaltfläche in meiner App klicken, ich zeige openwith intent und jetzt Benutzer wählt Adobe Reader, jetzt möchte ich die gleichen PDF-Dokument PDF-Bytes in Adobereader zeigen. Eigentlich bin ich newbee zu Android und nicht in der Lage zu starten oder zu identifizieren, welches Ereignis hier ausgelöst .... –

Antwort

3

Versuchen Sie, diese

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(); 

Nach pdf erstellen,

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 
} 

kann dies hilft Ihnen

EDIT 3

Der folgende Code wurde von mir getestet und es funktioniert wie u

die pdf-Datei erstellen möchten:

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"); 
    } 

    } 

Öffnen Sie die PDF-Datei:

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

fügen Sie die folgende Erlaubnis

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Note :
1.Change der Code, um als Sie wollen.

2. Rufen Sie createPDF() und dann OpenPDF() auf.

Dies ist Arbeitscode.

+0

Danke für die Antwort Sathish .. aber meine Datei ist nicht physisch verfügbar ,, Ich habe die Datei in Bytearray und Inputstream sowie ... –

+0

Dann erstellen Sie zuerst PDF von Ihrem ByteArray. siehe meine EDIT 1 –

+0

Was ist Dateipfad in Datei ... wo ich keine Datei habe stattdessen habe ich nur out.pdf ... –

0

wenn auf Openwith in App .. im mit Content-Provider angeklickt als meine Datei physikalisch nicht vorhanden ist ...

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;