1

一部のアプリケーション(Googleドライブ、ドロップボックスなど)からAndroidアプリケーションにpdf、docファイルを共有したいと思います。

私が試したこと

これらのコード スニペットを使用してアプリケーションで画像を共有できます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 を取得できますかこれらのファイルをアプリケーションで共有した場合のファイル パス。

4

1 に答える 1

0

上記のコードを使用して、タスクを実行できるように共有イメージ パスを取得できます

いいえ、いくつかの画像へのパスを取得できますMediaStore. すべての画像が によって索引付けされるわけではありませんMediaStore。すべての画像が、アプリがアクセスできるパスのファイルに保存されているわけではありません。

より一般的には、アクセスできるファイルによってすべてが表示されるわけcontent:// Uriではありません。ContentResolver代わりに、などの適切なメソッドを使用してコンテンツを使用する必要がありますopenInputStream()

.pdf、.doc ファイルをアプリで共有したいのですが、ファイルの正確な場所が必要です

によって索引付けされたファイルについてはMediaStore、 によって索引付けされた画像に対して行ったのと同じ解決策を使用してみてくださいMediaStore

于 2014-07-05T14:43:50.660 に答える