2011-07-27 51 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; 
}