2016-04-05 12 views
0

私にはいくつか問題があります。私は、ユーザーがボタンを押すと、pdfファイルがPDFリーダーで開かれるということを一つ試みます。私はプログラムのすべてを書きますが、うまくいきません。何が問題ですか?あなたは私にコードを正しく書くことができますか?私はすべてを断片化している。私のコード:PDFリーダーのrawフォルダからpdfファイルを開く

package lt.sviesioji.kdainiviesiojigimnazija; 

import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import java.io.File; 
import java.io.InputStream; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class FormulynasFragment extends Fragment { 

public FormulynasFragment() { 
} 

Button f; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    int backButtonCount = getFragmentManager().getBackStackEntryCount(); 

    if (backButtonCount > 0) { 
     Fragment newFragment = new PagrindinisFragment(); 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     transaction.replace(R.id.fragment_container, newFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 

    final View rootView = inflater.inflate(R.layout.fragment_formulynas, container, 
      false); 

    f = (Button) rootView.findViewById(R.id.button69); 
    f.setOnClickListener(new View.OnClickListener() { 
     InputStream is = getResources().openRawResource(R.raw.matematika); 

     @Override 
     public void onClick(View v) { 
      startpdf(); 
     } 
     private void startpdf() { 
      // TODO Auto-generated method stub 

      File file = new File("R.id.matematika"); 

      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) { 

       } 
      } 
     } 
    }); 
    return rootView; 
} 

}

答えて

0

あなたは、いくつかの問題を抱えています。

まず、R.id.matematikaはファイルではありません。これがリソースのIDです。リソースは開発マシン上のファイルのみです。 new File("R.id.matematika")はAndroid搭載端末では無意味です。

第2に、利用できるandroid.resource:Uriというスキームがありますが、それをサポートするアプリはほとんどありません。

第3に、RAWリソースをファイルにコピーできますが、ファイルsupport for that is fading awayを開くことができます。

あなたの主な選択肢があります。

  • コピーinternal storage上のファイルへのリソース、そしてuse FileProvider

  • 私が推薦するものの(ストレートその生のリソースからあなたのPDFを提供することができます使用my StreamProviderassets/に入れてください)

+0

だから、私はそのリソースをインターンどのように私はこのコードで行うことができますか? :) – iBoucher

関連する問題