安卓:浏览并从设备上传应用程序中的PDF或Word文件 [英] Android: Browse and upload a PDF or word file in application from the device

查看:228
本文介绍了安卓:浏览并从设备上传应用程序中的PDF或Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android移动应用程序中,用户可以上传他/她的简历和可再发送到服务器。

I am creating an android mobile application in which user can upload his/her resume and which can be then sent to the server.

简历可以在PDF格式或文字的形式。

Resume can be in pdf form or word form.

如何浏览PDF和Word文件,应给浏览这些文件,这些文件类型?像图像,我们有形象。

How to browse for the pdf and word files, which type should be given to browse these files? like for images we have image.

感谢。

推荐答案

使用此功能:

 private void getDocument()
        {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("application/msword,application/pdf");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            // Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test.
            startActivityForResult(intent, REQUEST_CODE_DOC);
        }

and onactivityresult:

    @Override
    protected void onActivityResult(int req, int result, Intent data)
    {
        // TODO Auto-generated method stub
        super.onActivityResult(req, result, data);
if (result == RESULT_OK)
        {
        Uri fileuri = data.getData();
                    docFilePath = getFileNameByUri(this, fileuri);
}
}

// get file path

    private String getFileNameByUri(Context context, Uri uri)
    {
        String filepath = "";//default fileName
        //Uri filePathUri = uri;
        File file;
        if (uri.getScheme().toString().compareTo("content") == 0)
        {
            Cursor cursor = context.getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.ORIENTATION }, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            cursor.moveToFirst();

            String mImagePath = cursor.getString(column_index);
            cursor.close();
            filepath = mImagePath;

        }
        else
            if (uri.getScheme().compareTo("file") == 0)
            {
                try
                {
                    file = new File(new URI(uri.toString()));
                    if (file.exists())
                        filepath = file.getAbsolutePath();

                }
                catch (URISyntaxException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
            {
                filepath = uri.getPath();
            }
        return filepath;
    }

我希望这会帮助你。

I hope this will help you.

这篇关于安卓:浏览并从设备上传应用程序中的PDF或Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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