31

これは私の最初の質問でした:

Androidに組み込まれているPDFビューアアプリを使用してアプリでPDFファイルを開くことができるようにしたいのですが、他のアプリを起動する方法がわかりません。開始アクティビティを呼び出す必要があると確信しています。開いているアプリを特定する方法と、その特定のアプリにファイルを渡す方法がわかりません。

誰か手がかりがありますか?

私の携帯電話にあるpdfビューアは実際にはHTCによって作成されており、AdobeはAndroid pdfビューアをほとんどリリースしていないことを知りました(これは素晴らしいことです)。したがって、新しい質問は次のとおりです。ユーザーがadobeのビューアをインストールしたことを確認するにはどうすればよいですか。次に、アプリからそのアプリでファイルを開くにはどうすればよいですか。

4

9 に答える 9

42

例外をキャッチすることなく、ユーザーのデバイスに適切なアプリケーションが存在するかどうかをプログラムで判断できます。

Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("path-to-document"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
    startActivity(intent);
} else {
    // Do something else here. Maybe pop up a Dialog or Toast
}
于 2012-05-21T16:37:45.523 に答える
16

Intentsちなみに、アドビは開発者に使用してほしいパブリックを文書化していません。

ファイル(SDカードまたはアプリローカルファイルストアのいずれか)をポイントし、MIMEタイプが。ACTION_VIEW Intentで試してみることができます。UriMODE_WORLD_READABLE"application/pdf"

于 2010-05-26T20:52:24.343 に答える
5
private void loadDocInReader(String doc)
     throws ActivityNotFoundException, Exception {

    try {
                Intent intent = new Intent();

                intent.setPackage("com.adobe.reader");
                intent.setDataAndType(Uri.parse(doc), "application/pdf");

                startActivity(intent);

    } catch (ActivityNotFoundException activityNotFoundException) {
                activityNotFoundException.printStackTrace();

                throw activityNotFoundException;
    } catch (Exception otherException) {
                otherException.printStackTrace();

                throw otherException;
    }
}
于 2011-10-31T11:27:18.480 に答える
3
            FileFinalpath = SdCardpath + "/" + Filepath + Filename;
            File file = new File(FileFinalpath);
            if (file.exists()) {
                Uri filepath = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(filepath, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } catch (Exception e) {
                    alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);             
                    Log.e("error", "" + e);
                }

            } else {
                alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);             

            }
于 2013-04-04T05:42:40.823 に答える
2

これはかなり古いトピックですが、外部のPDFリーダーアプリを使用してアセット/フォルダーにあるPDFを開くためのソリューションを次に示します。カスタムコンテンツプロバイダーを使用します:https ://github.com/commonsguy/cwac-provider

これを使用して、assets/またはres/raw/フォルダーから提供される任意のファイルを定義できます。

それを試してみてください!私がこれまでに見つけた最良かつ最も簡単な解決策。

于 2014-03-03T15:34:42.347 に答える
2

AndroidデバイスでPDFを表示しようとしたときにも同じ問題に直面し、最終的に解決策(サードパーティのPDFライブラリ統合)になりました

https://github.com/JoanZapata/android-pdfview

以下にリストされている複数のライブラリをテストしましたが、これらも機能していますが、

https://github.com/jblough/Android-Pdf-Viewer-Library

&nupdfはndkフレーバー(https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q=)に付属しており、NDKで抽出して使用する必要がありますjarまたはjavaなどのアプリケーションでこのライブラリの使用法を説明する素晴らしい記事@http://dixitpatel.com/integrating-pdf-in-android-application/

于 2015-08-17T10:25:52.130 に答える
2

Androidには、Android 5.0/Lollipopのフレームワークが組み込まれています。これはPDFRendererと呼ばれます。ユーザーがAndroid5.0を使用していると想定できる場合は、おそらくそれが最善の解決策です。

Googleの開発者サイトに公式の例があります:

http://developer.android.com/samples/PdfRendererBasic/index.html

注釈やその他のより高度な機能はサポートしていません。インテントを使用して完全なアプリを開くか、mupdfのようなSDKを埋め込むことに本当に戻った人のために。

(免責事項:私は非常にまれにmupdfで作業します。)

于 2016-04-11T18:37:09.113 に答える
0

回答としてマークされたものに加えて、manifest.xmlでこれらの権限が必要になります

****

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

****

于 2016-12-21T12:38:03.110 に答える
0

FLAG_GRANT_READ_URI_PERMISSIONを追加します

Intent intent = new Intent(Intent.ACTION_VIEW)
Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
intent.setDataAndType(outputFileUri, "application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Intent in = Intent.createChooser(intent, "Open File");
startActivity(in);

また、res-> xmlフォルダーにprovider_paths.xmlを追加し、マニフェストに以下のコードを追加する必要があります

<application>
   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                tools:replace="android:resource"
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
 </application>
于 2018-06-07T08:23:42.973 に答える