2013-11-24 67 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> 

希望這有助於任何人尋找答案