2011-07-27 128 views
1

我在我的應用程序,爲(我的應用程序內)打開畫廊行之有效的一些代碼,選擇一張照片,並上傳。Android圖庫「通過共享」和Intent.EXTRA_STREAM

我在集成處理與連接EXTRA_STREAMs,比如在圖片庫中的「共享」按鈕生成的意圖做了一個公平的嘗試。

這適用於我的Droid X,它適用於模擬器。

今天,我收到了用戶的錯誤報告,我用來拉的照片出來的MediaStore的光標返回null當我問它返回我的資源中提到的EXTRA_STREAM參數。代碼已經通過驗證Intent附加了EXTRA_STREAM的點,並且用戶告訴我他們正在使用庫中的「share」選項。

他們的設備是:

OS版本:2.3.3(10/GRI40) 設備:HTC PG86100

是怎麼回事?

爲什麼HTC畫廊會向我發送我無法訪問的EXTRA_STREAM的意圖?

是否有任何其他原因遊標將返回null?

String[] filePathColumn = {MediaColumns.DATA}; 

Uri selectedImageUri; 

//Selected image returned from another activity 
if(fromData){ 
    selectedImageUri = imageReturnedIntent.getData(); 
} else { 
    //Selected image returned from SEND intent 

    // This is the case that I'm having a problem with. 
    // fromData is set in the code that calls this; 
    // false if we've been called from an Intent that has an 
    // EXTRA_STREAM 
    selectedImageUri = (Uri)getIntent().getExtras().get(Intent.EXTRA_STREAM); 
} 

Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null); 
cursor.moveToFirst(); // <-- NPE on this line 

回答

3

事實證明,許多應用程序將發送EXTRA_STREAMs與圖像/ * MIME類型,但不是所有的人都用庫的內容提供商。

即分別生成一個空指針異常(如上)的那些是在我試圖我被賦予了文件時,從內容提供者閱讀的情況下:// EXTRA_STREAM。

,工程的代碼如下:

String filePath; 
String scheme = selectedImageUri.getScheme(); 

if(scheme.equals("content")){ 
    Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null); 
    cursor.moveToFirst(); // <--no more NPE 

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 

    filePath = cursor.getString(columnIndex); 

    cursor.close(); 

} else if(scheme.equals("file")){ 
    filePath = selectedImageUri.getPath(); 
    Log.d(App.TAG,"Loading file " + filePath); 
} else { 
    Log.d(App.TAG,"Failed to load URI " + selectedImageUri.toString()); 
    return false; 
}