19

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

4

6 に答える 6

14

これを処理する方法は次のとおりです。

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:"));
}
于 2012-12-28T02:47:43.530 に答える
13

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

File targetFile = new File(path);
                    Uri targetUri = Uri.fromFile(targetFile);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(targetUri, "application/*");
                    startActivityForResult(intent, DOC);
于 2013-01-02T06:27:35.620 に答える
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");
于 2015-01-19T11:37:42.013 に答える
2

Android 7.0 以下で .doc ファイルを開く完全な方法は次のとおりです。

ステップ-1: まず、次のスクリーンショットのように、pdf ファイルを assets フォルダーに配置します。 doc ファイルを assets フォルダーに配置する

ステップ-2: build.gradle ファイルに移動し、次の行を追加します。

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

}

次に、依存関係の下に次の行を追加して同期します。

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

ステップ-3: ここで、Like から拡張する必要がある新しい Java ファイルを追加します。FileProvider私の場合、ファイル名は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:"xml"フォルダーの下に名前を 付けたフォルダーを作成し"res"ます。(フォルダーが既に存在する場合は、作成する必要はありません)。providers_path.xml次に、フォルダーにファイルを追加しxmlます。スクリーンショットは次のとおりです。 provider_path xml ファイルの場所

ファイル内に次の行を追加します。

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

ステップ-5:ファイルに移動し、タグ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: ここで、pdf をロードする場所から Activity クラスに移動し、次の 1 行とこれら 2 つのメソッドを追加します。

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ファイル名を渡すと、ドキュメントリーダーアプリケーションでドキュメントファイルが開きます。.doc"chapter-0"

于 2017-08-22T12:06:13.983 に答える
0

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

もちろん、これは Word ドキュメント ビューアーが搭載されたデバイスでのみ機能します。

于 2012-04-20T08:03:14.680 に答える