Android如何在图库中查看SD卡中的文档?

5
我有一个应用程序,用户可以在其中从SD卡中搜索图像、音频、视频、文档文件,并选择1个文件上传到服务器。
使用以下代码,我可以打开画廊并选择图像、音频、视频,但我不知道如何从画廊中搜索文档。
以下是我的代码。
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);
    //intent.setType("video/*");
    //intent.setType("audio/*");
    //intent.setType("image/*");
    //**What I have to do for view document[.pdf/text/doc] file**
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_CODE);

有人知道如何实现这个吗?非常感谢任何帮助。
3个回答

1

尝试使用这个库 aFileChooser,它工作得很好。

请查看此链接


0
希望这能帮助您打开文档。
public void openDOC(String name) {


        File file = new File(Environment.getExternalStorageDirectory() + "/"
                + bmodel.getUserMasterBO().getUserid() + "/" + name);
        if (file.exists()) {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/msword");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "No Application Available to View DOC",
                        Toast.LENGTH_SHORT).show();
            }
        }

    }

谢谢回答。我想查看所有文件,然后从列表中选择单个文件。我不想打开文档。我只需要路径,这样我就可以将其上传到服务器。 - Nik88

0

请尝试以下方法:

File docfolder = new File(Environment.getExternalStorageDirectory() + "/"
                + "Documents/");
File docList[] = docfolder.listFiles();
for(int i=0;i<docList.length;i++)
{   
        if (docList[i].exists()) {
            Uri path = Uri.fromFile(docList[i]);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/msword");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            }
             catch (ActivityNotFoundException e) {

            }
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接