单击后如何在 arrayadapter 列表中打开文件?

How can I open the file in arrayadapter list after clicking?

我正在列出文件夹中的 PDF 文件。但我希望将它们命名为文件名,而不是 sdcard/mypath/files 另外,我想在通过 PDF 查看器单击它们时打开它们。我的代码:

public class activity1 extends ListActivity {

private List<String> fileList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    File root = new File("sdcard/mypath");
    ListDir(root);
}

void ListDir(File f) {
    File[] files = f.listFiles();
    fileList.clear();
    for (File file : files) {
        fileList.add(file.getPath());
    }
    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
    setListAdapter(directoryList);
}}
   public class activity1 extends ListActivity {
   ListView lv;
   private List<String> fileList = new ArrayList<String>();

  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    File root = new File("sdcard/mypath");
      lv = getListView();
        ListDir(root);
       }

     void ListDir(File f) {
      File[] files = f.listFiles();
   fileList.clear();
for (File file : files) {
    fileList.add(file.getName());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            viewPdf(fileList.get(i));
        }
    });
}

 }}

并在 pdf 默认 pdf 查看器中打开文件

     private void viewPdf(String file) {

        File pdfFile = new File(Environment.getExternalStorageDirectory() +    "/" + "mypath"+ "/" + file);
        Uri path = Uri.fromFile(pdfFile);

       // Setting the intent for pdf reader
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(path, "application/pdf");
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        startActivity(pdfIntent);
   }

使用上面的代码.. 希望对您有所帮助!

下面的代码将有助于打开这些类型的文件。检查这个答案:

 public static Intent getOpenFileIntent(Context ctx, String path) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        File file = new File(path);
        Uri uri = Uri.fromFile(file);
        String extension = MimeTypeMap.getFileExtensionFromUrl(path);
        if (extension != null) {
            String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            boolean status = canDisplay(ctx, type);
            if (type == null || !status) {
                type = "*/*";
            }
            if (!status) {
                Toast.makeText(ctx, "Cannot find appropriate Application", Toast.LENGTH_LONG).show();
            }
            intent.setDataAndType(uri, type);
        }
    } catch (Exception e) {
        AppLog.exception(e);
    }
    return intent;
}

以下代码将检查特定的 actionview 应用程序是否存在

   static boolean canDisplay(Context context, String mimeType) {
    try {
        PackageManager packageManager = context.getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType(mimeType);
        return packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    } catch (Exception e) {
        AppLog.exception(e);
    }
    return false;
}

如果文件类型可以打开,它会显示带有适当应用程序的文件或列出可以打开文件的应用程序。

更新

在将文件发送到此函数之前,请确保特定文件存在且可读。