应用程序加载从RES /生/ PDF文件的PDF文件 [英] app loads the pdf file from res/raw/pdf file

查看:227
本文介绍了应用程序加载从RES /生/ PDF文件的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI我尝试加载从原始文件夹中的文件APDF但是当我preSS的插嘴没有发生,请帮我与
。这是code

HI im trying to load apdf file from the raw folder but when i press the buttin nothing happen please help me with that . This is the code

公共类openpdf扩展活动
{

public class openpdf extends 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);
            }
        }
    );
}

}

推荐答案

下面是示例:

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

这篇关于应用程序加载从RES /生/ PDF文件的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆