2012-10-18 55 views
1

我通过http://www.vogella.com的链接帮助解决了我的问题,并进一步搜索。Android - 允许用户使用应用程序打开文本文件

•Android清单:

<uses-sdk android:minSdkVersion="8" /> 
<supports-screens android:resizeable="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" android:process="IPP.EZPadd"> 
    <activity 
     android:label="@string/app_name" 
     android:name=".EZPaddActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter > 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="file" /> 
      <data android:mimeType="text/plain" /> 
     </intent-filter> 
    </activity> 
</application> 

•Java的活动:

public void open_on_start() 
    { 
     Intent i = getIntent(); 
     Uri data = i.getData(); 
     if (data == null) 
     { 
      return; 
     } 
     URL url; 
     String startFile = ""; 
     try 
     { 
      url = new URL(data.getScheme(), data.getHost(), data.getPath()); 
      startFile = url.toString().replace("file:", ""); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(this, "Error:\n" + ex.getMessage().toString(), Toast.LENGTH_LONG).show(); 
      return; 
     } 
     if (startFile == null) 
     { 
      return; 
     } 
     StringBuilder text = new StringBuilder(); 
     can = false; 
     sel = false; 
     try 
     { 
      file = new File(CurDir, startFile); 
      reader = new BufferedReader(new FileReader(file)); 
      String line; 
      while ((line = reader.readLine()) != null) 
      { 
       text.append(line); 
       text.append('\n'); 
      } 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(this, "Error:\n" + e.getMessage().toString(), Toast.LENGTH_LONG).show(); 
     } 
     TextEditor.setText(text); 
     FileName.setText(startFile); 
    } 

谢谢西门的链接,这是非常有帮助的。我刚开始浏览它,这就是为什么我遇到这个问题,它没有加载文件。

回答

0

你不能这样做“默认”。只有用户可以选择(并删除或更改)操作的默认意图。否则,这将是混乱!

[编辑]

拉尔斯沃格尔总是一个很好的阅读。试试这个教程。这是因为它得到简单的(几乎)

http://www.vogella.com/articles/AndroidIntent/article.html

+0

那不是我的意思,我的意思是添加默认打开的选项。 – Timberwolf

+0

是你的问题,“如何让我的应用出现在应用列表中,以便在用户想要打开文本文件时处理特定的意图”?对不起,我认为我对你的问题感到困惑,因为你对这个主题很感兴趣;) – Simon

+0

以及如何处理通过我的活动传递的数据,他们使用我的应用程序打开它。 – Timberwolf

0

你需要一个意图过滤器添加到任何活动将处理文本文件。安装应用程序时,过滤器会在操作系统中注册。当用户尝试启动文件时,Android会询问您想要处理请求的注册应用程序。

+0

我从我做的研究中收集到了这个,我该如何去做呢? – Timberwolf

+0

本教程在Android开发人员页面上似乎完全符合您的需求。作为第一个结果,谷歌给了我。 http://developer.android.com/guide/components/intents-filters.html#npex – toadzky

相关问题