Android 图库“分享方式”和 Intent.EXTRA_STREAM

发布于 2024-11-26 16:59:11 字数 1143 浏览 1 评论 0原文

我的应用程序中有一些代码非常适合打开图库(从我的应用程序内)、选择照片并上传它。

我已经做出了公平的尝试,将对附加的 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

I have some code in my app that works well for opening the gallery (from within my app), selecting a photo, and uploading it.

I have made a fair attempt at integrating handling for intents with EXTRA_STREAMs attached, such as those generated by the "share" button in the gallery.

This works on my Droid X and it works on the emulator.

Today, I got an error report from a user; the cursor I used to pull the photo out of the MediaStore returned null when I asked it to return me the resource referred to in the EXTRA_STREAM parameter. The code had already passed through the point where it had verified that the Intent had an EXTRA_STREAM attached, and the user told me that they were using the "share" option from the gallery.

Their device was:

OS version: 2.3.3(10 / GRI40)
Device: HTC PG86100

What gives?

Why would the HTC gallery send me an Intent with an EXTRA_STREAM that I can't access?

Are there any other reasons that a cursor would return 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-12-03 16:59:11

事实证明,许多应用程序都会发送带有 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;
}

It turns out that many apps will send EXTRA_STREAMs with the image/* mime type, but not all of them use the gallery's content provider.

The ones that were generating a null pointer exception (as above) were cases in which I was trying to read from the content provider when I was given a file:// EXTRA_STREAM.

The code that works is as follows:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文