在Android中从外部SD卡读取PDF

时间:2016-09-13 17:02:00

标签: android pdf

我希望拥有所有来自我的Android手机的PDF文件我有以下代码相同,但它只提供我的手机内存的pdf文件而不是外部SD卡虽然我已经给出了

用于从外部SD卡获取pdf文件的plzz指南

   public class MainActivity extends Activity {

   File path;
   ListView list;
   static ArrayList<String> pdf_paths=new ArrayList<String>();
   static ArrayList<String> pdf_names=new ArrayList<String>();

  @Override
 protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   list=(ListView)findViewById(R.id.listView1);
   pdf_paths.clear();
   pdf_names.clear();

//Access External storage   

   path = new File(Environment.getExternalStorageDirectory() + "");
   searchFolderRecursive1(path);

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, pdf_names);
Log.e("aaaaaaaaaa", ""+pdf_names);

list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
 // TODO Auto-generated method stub
 String path = pdf_paths.get(arg2);
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
private static void searchFolderRecursive1(File folder)
{
 if (folder != null)
 {
  if (folder.listFiles() != null)
  {
   for (File file : folder.listFiles())
   {
    if (file.isFile())
    {
     //.pdf files 
     if(file.getName().contains(".mp3"))
      {
       Log.e("ooooooooooooo", "path__="+file.getName());
       file.getPath();
       pdf_names.add(file.getName());
       pdf_paths.add(file.getPath());
       Log.e("pdf_paths", ""+pdf_names);
      }
     } 
     else 
     {
      searchFolderRecursive1(file);
     }
    }
   }
  }
 }
}

1 个答案:

答案 0 :(得分:0)

试试这个,它可以正常工作 - 获取文件路径并将其作为新意图打开:

// get the file path from the external storage (SD card)
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/yourPdfName.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
// set the content type and data of the intent
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// start the intent as a new activity
startActivity(intent);