2016-07-04 4 views
4

私は自分のアプリケーションで開かれたpdfを持っています。ユーザーがボタンをクリックすると、application/pdfのopenwithオプションが表示されます。ユーザーは自分の選択(例えばadobe reader)を選択し、開かれたpdfファイルはユーザーの選択(この場合adobereader)で表示する必要があります。私は開いているPDFのために私と一緒にbytearrayと入力ストリームを持っています。openwith with openwithオプションandroid

+0

そして、あなたの質問は何ですか? – AxelH

+0

こんにちは、デフォルトで表示されます。これまでに何を試みましたか? –

+0

openwithウィンドウがデフォルトで表示されますが、アプリケーションのボタンをクリックするとopenwithの意図が表示され、ユーザーはadobe readerを選択します。これで、adobeReaderで同じpdfドキュメントのpdfバイトを表示します。実際に私の初心者はアンドロイドにして、ここでどのイベントが起きるのかを特定することができません。 –

答えて

3

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、PDFを作成した後、この

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

を試し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 
} 

この月は、PDFファイル作成

EDIT 3

次のコードは、自分で試験し、uは

をしたいと、それが働いているのに役立ちます:

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.Changeを追加君は 欲しいです。

2.call createPDF()とOpenPDF()。

これは動作コードです。

+0

返信ありがとうSathish ..しかし、私のファイルは物理的には利用できません。私はbytearrayとinputstreamのファイルも持っています... –

+0

最初にbyteArrayからpdfを作成します。私の編集を参照してください1 –

+0

ファイル内のファイルパスは何ですか...私はout.pdfを持っている代わりにファイルがありません... –

0

アプリで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;