2012-04-20 9 views
12

.doc拡張ファイルを開く方法はありますか?Android拡張子.docを開く方法

+1

ファイルを開いてどういう意味ですか?ファイルを表示するか、ファイルの内容を使用するかを選択します。 – MrWaqasAhmed

+0

私のアプリケーションのビュー – Narendra

+0

を試してみてくださいオープンmsワード、Excelファイル、pdfファイルビューアのアンドロイドマーケットのクイックオフィス – Dinesh

答えて

0

ファイルをRAWリソースからsdcardにコピーし、可読コピーを指すUriを持ち、適切なMIMEタイプを持つACTION_VIEWインテントでstartActivity()を呼び出すことができます。

もちろん、これはWord文書ビューアを持つデバイスでのみ機能します。

26

iOSと異なり、Android自体は.docまたは.pptファイルのレンダリングをサポートしていません。あなたは、あなたのアプリが他のアプリのアクティビティを再利用してこれらのドキュメントタイプを表示できるようにするための一般的な意図を探しています。しかし、これは、このインテントをサポートするアプリケーションがインストールされている電話機でのみ機能します。

http://developer.android.com/guide/topics/intents/intents-filters.html

かあなたには、いくつかのアプリをインストールしている場合は、このインテントを使用します。ここでは

//Uri uri = Uri.parse("file://"+file.getAbsolutePath()); 
Intent intent = new Intent(); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setAction(Intent.ACTION_VIEW); 
String type = "application/msword"; 
intent.setDataAndType(Uri.fromFile(file), type); 
startActivity(intent); 
+0

チャームのように働いた。 –

11

はあなたのために、このの世話をする方法である:

public void openDocument(String name) { 
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
    File file = new File(name); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); 
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
    if (extension.equalsIgnoreCase("") || mimetype == null) { 
     // if there is no extension or there is no definite mimetype, still try to open the file 
     intent.setDataAndType(Uri.fromFile(file), "text/*"); 
    } else { 
     intent.setDataAndType(Uri.fromFile(file), mimetype);    
    } 
    // custom message for the intent 
    startActivity(Intent.createChooser(intent, "Choose an Application:")); 
} 
9

オープンドキュメントから利用可能なアプリケーションのリスト ユーザーはアプリケーションの一覧からアプリケーションを選択する必要があります

File targetFile = new File(path); 
        Uri targetUri = Uri.fromFile(targetFile); 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(targetUri, "application/*"); 
        startActivityForResult(intent, DOC); 
+0

それでは、 'startActivityForResult()'の 'request_code'とは何ですか? DOCは特定の要求コードを表していません。 – CagCak

3

アプリ内で開く場合は、webviewでファイルを開くことができます。 例:ここでは

String doc="<iframe src='http://docs.google.com/viewer? url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'"+ 
    " width='100%' height='100%' style='border: none;'></iframe>"; 

     WebView wv = (WebView)findViewById(R.id.fileWebView); 
     wv.getSettings().setJavaScriptEnabled(true); 
     wv.getSettings().setAllowFileAccess(true); 
     //wv.loadUrl(doc); 
     wv.loadData(doc , "text/html", "UTF-8"); 
+0

ファイルプレビューを作成するためにどのようなオプションが必要だったのか見てみると、GoogleドキュメントにURLを送信できるかどうかわかりませんでした。 –

0

は、Android 7.0以下で.docファイルを開くための完全な方法である:

ステップ-1:すべての まず、資産のあなたのpdfファイルを置くようにフォルダ次のスクリーンショット。 Placing doc file in assets folder

ステップ-2: は今すぐファイルをbuild.gradleし、次の行を追加するために行く:

repositories { 
maven { 
    url "https://s3.amazonaws.com/repo.commonsware.com" 
} 

}

、その後、依存関係の下では、次の行との同期を追加します。

compile 'com.commonsware.cwac:provider:0.4.3' 

ステップ-3: FileProviderから拡張する必要がある新しいJavaファイルを追加します。私の場合、ファイル名はLegacyCompatFileProviderで、その内部にコードがあります。

import android.database.Cursor; 
import android.net.Uri; 

import android.support.v4.content.FileProvider; 

import com.commonsware.cwac.provider.LegacyCompatCursorWrapper; 

public class LegacyCompatFileProvider extends FileProvider { 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder))); 
    } 
} 

ステップ-4: "res"フォルダの下に"xml"という名前のフォルダを作成します。 (フォルダがすでにある場合は、作成する必要はありません)。今度はxmlフォルダにproviders_path.xmlというファイルを追加します。

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <files-path name="stuff" /> 
</paths> 

ステップ-5: Place of provider_path xml file

ファイル内の行以下を追加します。ここではスクリーンショットがある を今すぐAndroidManifest.xmlファイルに移動し、<application></application>タグの行を次のよう

<provider 
      android:name="LegacyCompatFileProvider" 
      android:authorities="REPLACE_IT_WITH_PACKAGE_NAME" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths"/> 
     </provider> 

ステップ6: 今度は、アクティビティクラスwh

private static final String AUTHORITY="REPLACE_IT_WITH_PACKAGE_NAME"; 

static private void copy(InputStream in, File dst) throws IOException { 
     FileOutputStream out=new FileOutputStream(dst); 
     byte[] buf=new byte[1024]; 
     int len; 

     while ((len=in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 

     in.close(); 
     out.close(); 
    } 

    private void LoadPdfFile(String fileName){ 

     File f = new File(getFilesDir(), fileName + ".doc"); 

     if (!f.exists()) { 
      AssetManager assets=getAssets(); 

      try { 
       copy(assets.open(fileName + ".doc"), f); 
      } 
      catch (IOException e) { 
       Log.e("FileProvider", "Exception copying from assets", e); 
      } 
     } 

     Intent i= 
       new Intent(Intent.ACTION_VIEW, 
         FileProvider.getUriForFile(this, AUTHORITY, f)); 

     i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

     startActivity(i); 
     finish(); 
    } 

LoadPdfFileメソッドを呼び出すと、私の場合"chapter-0"に似.docせずにファイル名を渡すと、それはドキュメントリーダアプリケーションでドキュメントファイルを開きます:EREあなたはPDFファイルをロードし、1つのライン及びこれらの2つの方法以下を追加します。

関連する問題