最近由於工作原因要在android下讀取rtf文件。在windows中可以通過word或寫字板直接打開RTF文件。
在android中也有類似的軟件可以打開,例如:thinkfree office
我們可以通過android調用 thinkfree office 打開rtf文件。
主要代碼如下:
public class HelloActivity extends Activity implements OnClickListener {
private Button button ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(this);
}
public void onClick(View v) {
String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
Intent intent = getRTFFileIntent(SDCardRoot + "11.rtf");
startActivity(intent);
}
public static Intent getRTFFileIntent( String param )
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param ));
intent.setDataAndType(uri, "application/rtf");
return intent;
}
}
在三星P1010上測試結果如下:
源碼下載: http://download.csdn.net/source/3462318