内容提供商不在一个加号设备中工作

时间:2018-01-25 06:34:18

标签: android android-contentprovider android-contentresolver oneplusone oneplustwo

我有两个应用程序,一个包含内容提供程序,另一个应用程序使用内容解析程序接收数据。如果我添加应该在第二个应用程序中从接收器显示的任何数据表单提供程序,这是预期的功能。但是在我从堆栈中删除第一个应用程序后添加数据然后第二个应用程序显示空游标,如果我将第一个应用程序保留在堆栈中,然后第二个应用程序显示正确的值。(此问题仅出现在一个加设备中

代码片段,其中光标值为null,

Cursor c = getContentResolver().query(CONTENT_URI, null, null, null,
            null);

2 个答案:

答案 0 :(得分:0)

可能会帮助你

 private String uriToFilename(Uri uri) {
    String path = null;

    if (Build.VERSION.SDK_INT < 11) {
        path = getRealPathFromURI_BelowAPI11(this, uri);
    } else if (Build.VERSION.SDK_INT < 19) {
        path = getRealPathFromURI_API11to18(this, uri);
    } else {
        path = getRealPathFromURI_API19(this, uri);
    }

    return path;
}

<强> BelowAPI11

 public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index
            = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

<强> API11to18

 public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    String result = null;
    CursorLoader cursorLoader = new CursorLoader(
            context,
            contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    if (cursor != null) {
        int column_index =
                cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
    }
    return result;
}

<强> API19

    public static String getRealPathFromURI_API19(Context context, Uri uri) {
        Log.e("uri", uri.getPath());
        String filePath = "";
        if (DocumentsContract.isDocumentUri(context, uri)) {
            String wholeID = DocumentsContract.getDocumentId(uri);
            Log.e("wholeID", wholeID);
// Split at colon, use second item in the array
            String[] splits = wholeID.split(":");
            if (splits.length == 2) {
                String id = splits[1];

                String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
                String sel = MediaStore.Images.Media._ID + "=?";
                Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        column, sel, new String[]{id}, null);
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
                cursor.close();
            }
        } else {
            filePath = uri.getPath();
        }
        return filePath;
    }

答案 1 :(得分:0)

启用“不优化设置”中的“解决一加问题”。

设置–>电池–>电池优化–>您的应用–>不优化 enter image description here