2012-03-02 5365 views
4

我拼命嘗試將捕獲的視頻發送到服務器。問題是由內置相機應用程序給出的URI不是真正的文件路徑。它看起來像這樣 - /content:/media/external/video/media/19如何從類似「/ content:/ media/external/video/media/19」的路徑中提取真實文件URI或文件數據?

如何從這種類型的URI直接訪問真實路徑或數據?

在閱讀了android文檔後,我看到它看起來像一個內容提供者的符號,但我仍然不知道如何達到我需要的數據。請幫忙!!!

在此先感謝

回答

3

我怎樣才能訪問真正的路徑,或直接從這種URI的數據?

你不知道。它可能不作爲文件存在。或者,它可能不存在,除非通過ContentProvider,否則您可以讀取該文件。

相反,使用ContentResolver打開上UriInputStream,並使用InputStream將數據傳輸到服務器。

+0

只是爲了顯示你的例子 - 這是我的代碼: 'InputStream爲= getContentResolver()openInputStream(Uri.parse(YOUR_URI_STRING)));'。 – maddob 2012-03-27 21:47:48

+0

如何獲取此類URI的類型?我的意思是如何知道它的PDF或PPT甚至WAV或MP3文件? – JRC 2012-05-28 10:56:44

+0

@JRC:如果你看看網頁的右上角,你會看到一個「Ask Question」按鈕。請使用它,而不是劫持其他問題。話雖如此,請問'MediaStore'爲您的內容的MIME_TYPE。 – CommonsWare 2012-05-28 10:59:43

1
public String getRealPathFromURI(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
    } finally { 
    if (cursor != null) { 
     cursor.close(); 
    } 
    } 
} 

見下職位 Get filename and path from URI from mediastore

相關問題