1

我的应用中有一些代码可以很好地打开画廊(从我的应用中),选择一张照片,然后上传它。

我已经做出了公平的尝试来集成对附加了 EXTRA_STREAM 的意图的处理,例如由图库中的“共享”按钮生成的那些。

这适用于我的 Droid X,也适用于模拟器。

今天,我收到一个用户的错误报告;当我要求它返回 EXTRA_STREAM 参数中引用的资源时,我用来从 MediaStore 中拉出照片的光标返回 null。代码已经通过了验证 Intent 附加了 EXTRA_STREAM 的点,用户告诉我他们正在使用图库中的“共享”选项。

他们的设备是:

操作系统版本:2.3.3(10 / GRI40) 设备:HTC PG86100

是什么赋予了?

为什么 HTC 图库会向我发送带有我无法访问的 EXTRA_STREAM 的 Intent?

游标返回 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
4

1 回答 1

3

事实证明,许多应用程序会发送带有 image/* mime 类型的 EXTRA_STREAM,但并非所有应用程序都使用图库的内容提供程序。

产生空指针异常(如上)的情况是,当我得到一个 file://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;
}
于 2011-08-23T00:09:31.223 回答