0

嗨,我正在尝试从原始文件夹加载 PDF 文件,但是当我按下按钮时没有任何反应,请帮助我。这是代码

公共类 openpdf 扩展 Activity {

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

}

4

2 回答 2

1

这是示例:

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();  
    }  
}  
于 2012-12-13T05:59:31.127 回答
0

只需使用它从原始文件夹中读取您的 pdf 文件

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

iS = resources.openRawResource(rID);  
于 2012-12-12T14:15:53.083 回答