2016-11-01 11 views
0

「pdfButton」をクリックするとpdfファイル(Androidフォンのダウンロードフォルダーにあります)を開きたい アクションを実行中に何も起こりません。ログに記録されたエラーまたはpdfファイルが表示されます。誰かが助けてくれますか?PDFファイルがAndroidで開かれていません

package com.mycompany.myfirstglapp; 
import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.SurfaceView; 
import android.webkit.WebView; 
import android.widget.Toast; 
import java.io.File; 

/** 
* Created by admin on 1/11/2016. 
*/ 

public class PdfActivity extends Activity { 
    private SurfaceView surface; 
    Button pdfButton; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pdf); 
     surface = (SurfaceView) findViewById(R.id.pdfSurface); 
     pdfButton = (Button) findViewById(R.id.pdfView); 

     pdfButton .setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        // On click will call the showPdf method to display the pdf file in sd card or downloads 

        showPdf(view); 
       } 
      }); 



    } 


    public void showPdf(View view) { 

     // The pdf file [LawsofthegamewebEN_Neutral.pdf] is avaialble in Android > Downloads folder. 

     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/LawsofthegamewebEN_Neutral.pdf"); 

     if (file.exists()) { 
      Uri path = Uri.fromFile(file); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      try { 
       startActivity(intent); 
      } 
      catch (ActivityNotFoundException e) { 
       Toast.makeText(PdfActivity.this, 
         "No Application Available to View PDF", 
         Toast.LENGTH_SHORT).show(); 
      } 
     } 

    } 



} 

答えて

1

、あなたのデバッガでコードをステップ実行、またはでより多くのログ・ステートメントを入れた場合、私はあなたがそのfile.exists()戻りfalseを見つけるだろうと思われます。そして、現時点では、あなたはその場合何もしません。

私はあなたのコードを見ているされていない(Android携帯電話でダウンロードフォルダで利用可能です)pdfファイル

を開きたいです。置き換え:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/LawsofthegamewebEN_Neutral.pdf"); 

で:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "LawsofthegamewebEN_Neutral.pdf"); 

また、あなたのfile.exists()呼び出しはあなたがREAD_EXTERNAL_STORAGE権限を保持する必要があることを意味していることに注意してください。

+0

私は結果を投稿しようとします – soccerway

+0

問題を解決しました:) – soccerway

関連する問題