从电子邮件中打开附加的文本文件

时间:2012-05-05 21:38:51

标签: android android-intent

我想要做的事情看起来很简单但是我在某处遗漏了一些细节。我的目标是创建一个可以打开附加到电子邮件的文本文件的应用程序。我给自己发了一封电子邮件,附有一个文本文件,文件扩展名为TRE。我创建了一个Android应用程序,其中包含将在启动应用程序的清单中指定的意图。它做得很好。我在OnCreate函数中有代码从intent获取数据以获取文件名。这是它变得奇怪的地方。当我打开电子邮件时,它会将附件保存到temp-10.tre。但是,当应用程序打开时,它说文件名为/ 4/473 / RAW。当然,当我尝试打开这个文件时,它说无法打开它。我怀疑这个文件名不正确。

以下是清单代码:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TREViewerActivity"
        android:label="@string/app_name" android:screenOrientation="landscape">
        <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:mimeType="*/*" /> 
            <data android:pathPattern=".*\\.tre" />     
        </intent-filter> 

    </activity>
</application>

以下是我用来获取文件名的代码:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    strFilename = "Not set";
    Intent intent = getIntent();
    if(intent != null)
    {
        Uri uri = intent.getData();
        if(uri != null)
        {
            strFilename = intent.getData().getPath();
            readFile();             
        }
    }

以下是我用来打开文件的内容:

private void readFile()
{
    try
    {
        FileInputStream inFile = new FileInputStream(strFilename);
        inFile.close();
        Toast.makeText(getApplicationContext(),"File opened ok",Toast.LENGTH_SHORT).show();         
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(),e.toString() +   e.getMessage(),Toast.LENGTH_SHORT).show();          
    }

}

任何想法我做错了什么?

0 个答案:

没有答案