【问题标题】:How to share .pdf,.doc file into my application如何将 .pdf、.doc 文件共享到我的应用程序中
【发布时间】:2014-07-05 14:34:02
【问题描述】:

我想将某些应用程序(例如 google drive、dropbox)中的 pdf、doc 文件共享到我的 android 应用程序。

我尝试了什么

我可以使用这些代码 sn-p 将图像共享到我的应用程序中,我可以使用 AndroidManifest.xml 中的以下代码获取该图像路径。

<intent-filter>
            <action android:name="android.intent.action.SEND" />



                 <data android:mimeType="image/*" /> 


            <category android:name="android.intent.category.DEFAULT" />


            </intent-filter>

在我的活动中

Intent myintent = getIntent();
            Bundle extras = myintent.getExtras();
            String action = myintent.getAction();

            // if this is from the share menu
            if (Intent.ACTION_SEND.equals(action)) {   if (extras.containsKey(Intent.EXTRA_STREAM)) {
            // Get resource path
            Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
            String filename = parseUriToFilename(uri);

            if (filename != null) {


                doing some process here//

            }
            }
            }

public String parseUriToFilename(Uri uri) {
            String selectedImagePath = null;
            String filemanagerPath = uri.getPath();

            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor =  getContentResolver().query(uri, projection, null, null, null);

            if (cursor != null) {
            // Here you will get a null pointer if cursor is null
            // This can be if you used OI file manager for picking the media
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            selectedImagePath = cursor.getString(column_index);
            }

            if (selectedImagePath != null) {
            return selectedImagePath;
            }
            else if (filemanagerPath != null) {
            return filemanagerPath;
            }
        return null;
            }

我真正需要的东西

使用上面的代码,我可以获得共享图像路径,以便我可以执行我的任务。为此,我想将 .pdf、.doc 文件共享到我的应用程序中,我需要文件的确切位置。所以我该如何获取pdf,doc 文件路径,如果我将这些文件共享到我的应用程序中。

【问题讨论】:

    标签: android pdf android-intent android-manifest android-sharing


    【解决方案1】:

    使用上面的代码,我可以获得共享图像路径,以便我可以执行我的任务

    不,您可以获得 一些 图像的路径,这些图像恰好被 MediaStore 索引。并非所有图像都由MediaStore 索引。并非所有图像都存储在您的应用可以访问的路径的文件中。

    更一般地说,not every content:// Uri that you will find will be represented by a file that you can access。相反,您需要使用适当的ContentResolver 方法来使用内容,例如openInputStream()

    为此,我想将 .pdf、.doc 文件共享到我的应用程序中,我需要文件的确切位置

    对于那些碰巧也被MediaStore 索引的文件,欢迎您尝试使用与MediaStore 索引的图像相同的解决方案。

    【讨论】:

    • 如何获取pdf文件路径。?您能否提供任何获取文件路径的示例代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多