2013-11-24 143 views
0

我有一个应用程序应该能够打开并从电子邮件中读取文本文件。 我改变了我的清单以下从uri获取文件名和路径作为文本文件

<activity 
android:name="FileLoaderActivity" 
    android:label="@string/fileloader_title" 
    android:parentActivityName="MainActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <data android:mimeType="text/plain"/> 
    </intent-filter> 
</activity> 

在我FileLoaderActivity我见的onCreate方法中以下内容:

// Get the intent that started this activity 
Intent intent = getIntent(); 
Uri data = intent.getData(); 

//Verify that the type is text file 
if (intent.getType().equals("text/plain")) { 
    String path = data.getPath(); 
    File file = new File(path); 
    Toast.makeText(getApplicationContext(), path,Toast.LENGTH_SHORT).show(); 
    if(file.exists()) 
     Toast.makeText(getApplicationContext(), "I exist",Toast.LENGTH_SHORT).show(); 
} 

现在输出如下:

/MY_EMAIL @ gmail.com/messages/78/attachments/0.1/BEST/false

和“我存在”永远不会出现。因此,这不会打开文件当然 我已经阅读了很多其他的解决方案,但他们似乎是媒体的具体。 能否请你帮我把实际的文件路径,

非常感谢你提前

回答

1

好了,所以你可以只使用

public String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    @SuppressWarnings("deprecation") 
    Cursor cursor = managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

这不仅只是图像或媒体文件。当然,你必须首先下载文件,然后进入你的下载文件夹才能打开它。此外,如果你尝试加载一个文件,请确保您编辑清单有正确的权限,如下所示:

<manifest> 
... 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
</manifest> 

希望这有助于任何人寻找答案