从资产文件夹打开PDF

时间:2014-05-24 08:14:50

标签: android-layout pdf android-intent android-listview android-assets

我必须在列表视图的点击事件中从资产文件夹打开PDF。我有代码首先将其移动到SD卡。但每当我在资产文件夹中插入新的PDF时,我必须编写完整的代码来打开PDF,在资源文件夹中插入PDF是不可能的,它只需将其复制到SD卡中并打开它。

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.issue_2014);
CopyReadAssets();
AssetManager asset = getAssets();
try {
final String[] arrdata = asset.list("PDFfolder");
List<String> pdflist = new ArrayList<String>();
int size = arrdata.length;
for(int i = 0;i<size;i++)
{
  if(arrdata[i].contains(".pdf"))

  {
    pdflist.add(arrdata[i]); 
   }
}
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, R.layout.list,R.id.title,pdflist);
ListView listView = (ListView) findViewById(R.id.issue_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
     public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        if(position == 3 ) {

             Uri path = Uri.parse("file://" + getFilesDir() + "/abc.pdf");
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(path, "application/pdf");

             try{
             startActivity(intent);    
             }
             catch(Exception e){
                 Toast.makeText(Issue_2014.this,e.toString(),Toast.LENGTH_LONG).show();
             }

             }
        if(position == 2 ) {


        Uri path = Uri.parse("file://" + getFilesDir() + "/aai.pdf");
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(path, "application/pdf");

             try{
             startActivity(intent);    
             }
             catch(Exception e){
                 Toast.makeText(Issue_2014.this,e.toString(),Toast.LENGTH_LONG).show();
             }

             }
        if(position == 0 ) {

        Uri path = Uri.parse("file://" + getFilesDir() + "/Silence.pdf");
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(path, "application/pdf");

             try{
             startActivity(intent);    
             }
             catch(Exception e){
                 Toast.makeText(Issue_2014.this,e.toString(),Toast.LENGTH_LONG).show();
             }

             }
        if(position == 1) {


             Uri path = Uri.parse("file://" + getFilesDir() + "/Tawzihulmasael.pdf");
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(path, "application/pdf");

             try{
             startActivity(intent);    
             }
             catch(Exception e){
                 Toast.makeText(Issue_2014.this,e.toString(),Toast.LENGTH_LONG).show();
             }

             }

             }

        });


} catch (IOException e) {


}

ImageButton ib=(ImageButton)findViewById(R.id.listimage);
ib.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
         myWebLink.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
         myWebLink.setData(Uri.parse("http://www.curenfly.com"));
         startActivity(myWebLink);

    }
});
}

@SuppressLint("WorldReadableFiles")
@SuppressWarnings("deprecation")
private void CopyReadAssets() {
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "abc.pdf");
try
{
in = assetManager.open("PDFfolder/abc.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{

}
File f2 = new File(getFilesDir(), "aai.pdf");
try
{
in = assetManager.open("PDFfolder/aai.pdf");
out = openFileOutput(f2.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{

}
File f3 = new File(getFilesDir(), "Silence.pdf");
try
{
in = assetManager.open("PDFfolder/Silence.pdf");
out = openFileOutput(f3.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{

}
File f4 = new File(getFilesDir(), "Tawzihulmasael.pdf");
try
{
in = assetManager.open("PDFfolder/Tawzihulmasael.pdf");
out = openFileOutput(f4.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{

}

}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
}

现在在上面的代码我已经在assets文件夹中插入了4个Pdfs。并为四个不同的位置写了四次代码,将PDF复制到SD卡并打开它。现在,如果我插入第5版PDF,那么我必须编写这整个杂乱的东西来复制和打开PDF。 任何帮助,将不胜感激... Plz帮我解决了... ...

由于

0 个答案:

没有答案