2012-12-12 73 views
0

嗨即时尝试从原始文件夹中加载apdf文件,但是当我按下buttin没有任何事情发生时,请帮助我与那 。这是代码应用程序从res/raw/pdf文件加载pdf文件

公共类openpdf扩展活动 {

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button button = (Button) findViewById(R.id.pdfbutton); 
    button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 

       File pdfFile = new File(
        "android.resource://com.mycompany.myapp.PDF/" 
        + R.raw.adham); 
       Uri path = Uri.fromFile(pdfFile); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       intent.setDataAndType(path, "application/pdf"); 

       startActivity(intent); 
      } 
     } 
    ); 
} 

}

+0

你试过调试吗?什么都没有发生什么意思? –

+0

当我按下按钮它dosnt做任何事情就像没有命令的按钮 –

+0

pdf文件dosnt显示到屏幕上没有任何错误 –

回答

0

只是用它来从原始文件夹

int rID = resources.getIdentifier("fortyonepost.com.lfas:raw/"+fileName, null, null); 

iS = resources.openRawResource(rID); 
+0

我现在就试试吧 –

+0

你可以写所有的代码请...我在编程新编程,我不知道在哪里把你给我的代码谢谢你,对不起 –

1

在这里阅读PDF文件是样本:

package fortyonepost.com.lfas;//Created by DimasTheDriver Apr/2011. 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import android.app.Activity; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

public class LoadFromAltLoc extends Activity { 

    //a handle to the application's resources 
    private Resources resources; 
    //a string to output the contents of the files to LogCat 
    private String output; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //get the application's resources 
     resources = getResources(); 

     try 
     { 
      //Load the file from the raw folder - don't forget to OMIT the extension 
      output = LoadFile("from_raw_folder", true); 
      //output to LogCat 
      Log.i("test", output); 
     } 
     catch (IOException e) 
     { 
      //display an error toast message 
      Toast toast = Toast.makeText(this, "File: not found!", Toast.LENGTH_LONG); 
      toast.show(); 
     } 

     try 
     { 
      //Load the file from assets folder - don't forget to INCLUDE the extension 
      output = LoadFile("from_assets_folder.pdf", false); 
      //output to LogCat 
      Log.i("test", output); 
     } 
     catch (IOException e) 
     { 
      //display an error toast message 
      Toast toast = Toast.makeText(this, "File: not found!", Toast.LENGTH_LONG); 
      toast.show(); 
     } 
    } 

    //load file from apps res/raw folder or Assets folder 
    public String LoadFile(String fileName, boolean loadFromRawFolder) throws IOException 
    { 
     //Create a InputStream to read the file into 
     InputStream iS; 

     if (loadFromRawFolder) 
     { 
      //get the resource id from the file name 
      int rID = resources.getIdentifier("fortyonepost.com.lfas:raw/"+fileName, null, null); 
      //get the file as a stream 
      iS = resources.openRawResource(rID); 
     } 
     else 
     { 
      //get the file as a stream 
      iS = resources.getAssets().open(fileName); 
     } 

     //create a buffer that has the same size as the InputStream 
     byte[] buffer = new byte[iS.available()]; 
     //read the text file as a stream, into the buffer 
     iS.read(buffer); 
     //create a output stream to write the buffer into 
     ByteArrayOutputStream oS = new ByteArrayOutputStream(); 
     //write this buffer to the output stream 
     oS.write(buffer); 
     //Close the Input and Output streams 
     oS.close(); 
     iS.close(); 

     //return the output stream as a String 
     return oS.toString(); 
    } 
} 
相关问题