마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

ContentProvider의 Uri를 ContentResolver가 인식을 못합니다

0 추천
public class MyContentProvider extends ContentProvider {

    private static final String AUTHORITY = "com.example.dh.myapplication.provider.MyContentProvider";
    private static final String TABLE_INFORMATION = "informations";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + File.separator + TABLE_INFORMATION);

    public static final int INFORMATIONS = 1;
    public static final int INFORMATIONS_ID = 2;

    private MyDBHandler handler;

    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        matcher.addURI(AUTHORITY, TABLE_INFORMATION, INFORMATIONS);
        matcher.addURI(AUTHORITY, TABLE_INFORMATION + File.separator + "#", INFORMATIONS_ID);
    }

    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int uriType = matcher.match(uri);
        SQLiteDatabase db = handler.getWritableDatabase();
        int rowsDeleted = 0;

        switch (uriType) {
            case INFORMATIONS:
                rowsDeleted = db.delete(TABLE_INFORMATION, selection, selectionArgs);
                break;
            case INFORMATIONS_ID:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsDeleted = db.delete(TABLE_INFORMATION, MyDBHandler.COLUMN_ID + "=" + id, null);
                } else {
                    rowsDeleted = db.delete(TABLE_INFORMATION, MyDBHandler.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown URI :" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsDeleted;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int uriType = matcher.match(uri);

        SQLiteDatabase db = handler.getWritableDatabase();

        long id = 0;

        switch (uriType) {
            case INFORMATIONS:
                id = db.insert(TABLE_INFORMATION, null, values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI :" + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse(TABLE_INFORMATION + File.separator + id);
    }

    @Override
    public boolean onCreate() {
        handler = new MyDBHandler(getContext(), null)
        ;
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(TABLE_INFORMATION);
        int uriType = matcher.match(uri);
        switch (uriType) {
            case INFORMATIONS:
                break;
            case INFORMATIONS_ID:
                builder.appendWhere(MyDBHandler.COLUMN_ID + "=" + uri.getLastPathSegment());
                break;
            default:
                throw new IllegalArgumentException("Unknown URI :" + uri);
        }
        Cursor cursor = builder.query(handler.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int uriType = matcher.match(uri);
        SQLiteDatabase db = handler.getWritableDatabase();
        int rowsUpdated = 0;

        switch (uriType) {
            case INFORMATIONS:
                rowsUpdated = db.update(TABLE_INFORMATION, values, selection, selectionArgs);
                break;
            case INFORMATIONS_ID:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsUpdated = db.update(TABLE_INFORMATION, values, MyDBHandler.COLUMN_ID + "=" + id, null);
                } else {
                    rowsUpdated = db.update(TABLE_INFORMATION, values, MyDBHandler.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown URI :" + uri);
        }
        getContext().getContentResolver().notifyChange(uri,null);
        return rowsUpdated;
    }
}

이게 CP의 코드이구요 매니페스트에 등록 했습니다.

public void add(View v) {
        ContentValues values = new ContentValues();

        String site = textSite.getText().toString();
        String name = textName.getText().toString();
        String password = textPassword.getText().toString();

        values.put(COLUMN_SITE, site);
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_PASSWORD, password);
        Uri uri = resolver.insert(CONTENT_URI, values);
        Toast.makeText(this, "추가됨. Uri : \n" + uri, Toast.LENGTH_SHORT).show();
    }

이부분이 데이터를 엑세스하려는 또다른 앱의 함수인데 이 함수를 호출하면

이렇게 오류가 뜨네요 CP앱의 CONTENT_URI를 그대로 복사해서 작성해서 틀릴리가 없는데 왜이럴까요

고수님들 부탁드립니다!

지리산암반수 (180 포인트) 님이 2016년 9월 21일 질문
manifest에 등록한 provider 구문좀 올려주실수 있을까요?

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...