内容提供者在Nexus系列设备上无法工作

11
我正在开发一个带有ContentProvider的应用程序,用于提供一些内部文件(二进制文件)。当我在三星Galaxy S、SII或其他任何设备上部署它时,它都能正常工作,但是当我在Galaxy Nexus或Nexus S上尝试时,它就无法工作了!
情景:
我的ContentProvider可以使用两个URI访问。根据此URI,提供程序会创建DataCursor(扩展CrossProcessCursor)或ModelCursor(也扩展CrossProcessCursos)。事实是,在Nexus系列中,我访问第一个游标(DataCursor)以检索标识符,并且它能够完美地工作,但是当访问第二个游标时,尝试使用"getBlob()"方法时总是抛出"OutOfBoundsException"异常。 提供程序
@Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;

        // If the third app requieres DATA (phrase id, phrase string and phrase name)
        if(uri.toString().equalsIgnoreCase(ProviderConstants.DATA_URI.toString())) {
            // Build the DataHelper and the customized cursor
            DataHelper dataHelper = new DataHelper(getContext());
            cursor = new DataCursor(dataHelper);
        } else if(uri.toString().equalsIgnoreCase(ProviderConstants.MODEL_URI.toString())) {            
            // Let's take the model id from the selectionArgs...
            if (selectionArgs != null && selectionArgs.length > 0) {
                String modelId = selectionArgs[0];

                // Get an instance to the persistent storage service...
                File file = FileManager.getDirectory(getContext(), modelId);
                FileSystemPersistentStorageService clientPersistentStorageService = new FileSystemPersistentStorageService(file);

                cursor = new ModelCursor(clientPersistentStorageService);
            } else {
                Log.e("ContentProvider", "Query without model id on selectionArgs");
            }
        }

        return cursor;
    }

如果你需要任何代码或其他东西,请直接提出请求!

非常感谢。


首先:1. 您的Android设备上有哪些确切的SDK版本? 2. 每个设备上的确切SQLite版本是什么(adb shell sqlite3 --version)? 3. 代码在模拟器中是否正常工作? 4. 当您收到OutOfBoundsException时,完整的调用堆栈是什么? 5. ModelCursor是如何实现的? 6. 您的数据库模型是什么? 7. 您如何使用您的内容提供程序(代码)? - vArDo
  1. 从2.3.3到4.0(广泛的设备范围)
  2. 这有关系吗?我可以检查它们,但ModelCursor仅从内部文件存储中获取数据而不是从SQLite中获取。
  3. 【待测试,谢谢】。
  4. 【链接】(http://pastebin.com/NS7zBSGx)。
  5. 只需使用getBlob方法和其他所需方法[链接](http://pastebin.com/q4kMAEw3)。
  6. 不需要数据库。
  7. Cursor modelCursor = getContentResolver().query(Uri.parse("content://" + PROVIDER_NAME + "/model"), null, null, new String[] {modelId}, null);- modelCursor.getBlob(0);
- manelizzard
2
你尝试将光标移动到第一个条目了吗?(modelCursor.moveToFirst()) - SteveR
2个回答

1

终于我得到了答案。问题不是由于设备,而是由于Android操作系统版本。

早期的4.0.3版本实现了AbstarctCursor的fillWindow()方法,其中一种方式是不需要将光标位置设置为第一个项目的位置。在4.0.3之后(如此看来),该方法被修改,导致未将光标位置设置为0。

抛出的异常是:

07-31 11:35:09.938: W/System.err(2760): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
07-31 11:35:09.985: W/System.err(2760):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
07-31 11:35:09.989: W/System.err(2760):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-31 11:35:09.989: W/System.err(2760):     at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:44)
07-31 11:35:09.993: W/System.err(2760):     at android.database.CursorWrapper.getBlob(CursorWrapper.java:122)
07-31 11:35:09.993: W/System.err(2760):     at es.agnitio.kivox.mobile.internal.ModelImporterAndroidImpl.importModelPackage(ModelImporterAndroidImpl.java:44)
07-31 11:35:09.993: W/System.err(2760):     at es.agnitio.kivox.mobile.internal.KivoxMobileBasicCode.importModel(KivoxMobileBasicCode.java:159)
07-31 11:35:09.997: W/System.err(2760):     at es.agnitio.kivox.mobile.internal.KivoxMobileBasicImpl.importModel(KivoxMobileBasicImpl.java:47)
07-31 11:35:09.997: W/System.err(2760):     at es.agnitio.kivoxmobile.testing.KivoxMobileBasicTrial.onCreate(KivoxMobileBasicTrial.java:63)
07-31 11:35:09.997: W/System.err(2760):     at android.app.Activity.performCreate(Activity.java:5008)
07-31 11:35:10.001: W/System.err(2760):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-31 11:35:10.001: W/System.err(2760):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-31 11:35:10.001: W/System.err(2760):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-31 11:35:10.004: W/System.err(2760):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-31 11:35:10.004: W/System.err(2760):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-31 11:35:10.004: W/System.err(2760):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 11:35:10.008: W/System.err(2760):     at android.os.Looper.loop(Looper.java:137)
07-31 11:35:10.008: W/System.err(2760):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-31 11:35:10.008: W/System.err(2760):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:35:10.012: W/System.err(2760):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:35:10.012: W/System.err(2760):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 11:35:10.012: W/System.err(2760):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 11:35:10.016: W/System.err(2760):     at dalvik.system.NativeStart.main(Native Method)

经过深入研究,发现光标索引在fillWindow()中未被设置。 解决方案 为解决这个"碎片化"问题,在自定义光标的moveToFirst()move()方法(根据应用逻辑)中添加 moveToPosition(0)。这将使光标索引被设置为0,避免了"-1"索引请求异常。
希望能对大家有所帮助 =)

0

我对这个问题有点需要预测能力,但是...你可以尝试两件事情:

  • SteveR建议的那样将光标移动到第一个条目。

  • 其次,在使用 Dell Streak 7" 平板电脑时,我遇到了类似的问题。它被认为是设备列表中具有特定异常的一部分,涉及访问其内部存储。虽然很棘手,但通过 if 语句进行了排序。可能是类似的问题,但我怀疑,特别是 Nexus 应该是开发者的设备。

也许你应该尝试运行最简单的 ContentProvider 测试应用程序,并查看它在这些设备上的表现如何。


抱歉回复晚了。我已经测试过所有内容了。问题在于 moveToFirst() 返回了 false。就好像系统无法找到提供程序一样,很奇怪,因为在其他设备上它可以找到。 - manelizzard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接