0

I'm trying to add the save functionality to my note app via the method

 public boolean saveNote()
    {
        String title=mTitleEditText.getText().toString();
        if(TextUtils.isEmpty(title))
        {
            mTitleEditText.setError("Title is required");
            return false;

        }
        String content=mContentEditText.getText().toString();
        if(TextUtils.isEmpty(content))
        {
            mContentEditText.setError("Content is required");
            return false;
        }

            Log.e(TAG,"Creating new Note");
            Note note = new Note();
            note.setTitle(title);
            note.setContent(content);
            NoteManager.newInstance(getActivity()).create(note);

        return true;
    }

In the NoteManager class I have create method -

 public long create(Note note) {
        ContentValues values = new ContentValues();
        values.put(Constants.COLUMN_TITLE, note.getTitle());
        values.put(Constants.COLUMN_CONTENT, note.getContent());
        values.put(Constants.COLUMN_CREATED_TIME, System.currentTimeMillis());
        values.put(Constants.COLUMN_MODIFIED_TIME, System.currentTimeMillis());
        Uri result = mContext.getContentResolver().insert(NoteContentProvider.CONTENT_URI, values); // line 39 in LOGCAT
        long id = Long.parseLong(result.getLastPathSegment());
        return id;
    }

I get the following error when I try to save and create a new note

java.lang.IllegalArgumentException: Unknown URL content:/com.example.android.sqlitedbase.data.NoteContentProvider/notes
                                                                                     at android.content.ContentResolver.insert(ContentResolver.java:862)
                                                                                     at com.example.android.sqlitedbase.data.NoteManager.create(NoteManager.java:39)
                                                                                     at com.example.android.sqlitedbase.fragments.NoteLinedEditorFragment.saveNote(NoteLinedEditorFragment.java:169)

My DataBaseHelper class

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "simple_note_app.db";
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_NOTE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Constants.NOTES_TABLE);
        onCreate(db);
    }



    private static final String CREATE_TABLE_NOTE = "create table "
            + Constants.NOTES_TABLE
            + "("
            + Constants.COLUMN_ID + " integer primary key autoincrement, "
            + Constants.COLUMN_TITLE + " text not null, "
            + Constants.COLUMN_CONTENT + " text not null, "
            + Constants.COLUMN_MODIFIED_TIME + " integer not null, "
            + Constants.COLUMN_CREATED_TIME + " integer not null " + ")";


}

NoteContentProvider

public class NoteContentProvider extends ContentProvider {
    private DatabaseHelper dbHelper;
private static final String TAG="adf";
    private static final String BASE_PATH_NOTE = "notes";
    private static final String AUTHORITY = "com.example.android.sqlitedbase.data.NoteContentProvider";
   // public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_NOTE);
    public static final Uri CONTENT_URI = Uri.parse("content:/" + AUTHORITY + "/" + BASE_PATH_NOTE);
    private static final int NOTE = 100;
    private static final int NOTES = 101;

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(AUTHORITY, BASE_PATH_NOTE, NOTES);
        URI_MATCHER.addURI(AUTHORITY, BASE_PATH_NOTE + "/#", NOTE);

    }

    private void checkColumns(String[] projection) {
        if (projection != null) {
            HashSet<String> request = new HashSet<String>(Arrays.asList(projection));
            HashSet<String> available = new HashSet<String>(Arrays.asList(Constants.COLUMNS));
            if (!available.containsAll(request)) {
                throw new IllegalArgumentException("Unknown columns in projection");
            }
        }
    }

    @Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        checkColumns(projection);

        int type = URI_MATCHER.match(uri);
        switch (type){
            case NOTE:
                //there is not to do if the query is for the table
                break;
            case NOTES:
                queryBuilder.appendWhere(Constants.COLUMN_ID + " = " + uri.getLastPathSegment());
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int type = URI_MATCHER.match(uri);
        Log.e(TAG,"The uri type is "+type);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Long id;
        switch (type){
            case NOTES:
                id = db.insert(Constants.NOTES_TABLE, null, values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return Uri.parse(BASE_PATH_NOTE + "/" + id);
    }


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int type = URI_MATCHER.match(uri);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int affectedRows;
        switch (type) {
            case NOTES:
                affectedRows = db.delete(Constants.NOTES_TABLE, selection, selectionArgs);
                break;

            case NOTE:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    affectedRows = db.delete(Constants.NOTES_TABLE, Constants.COLUMN_ID + "=" + id, null);
                } else {
                    affectedRows = db.delete(Constants.NOTES_TABLE, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRows;
    }


    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int type = URI_MATCHER.match(uri);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int affectedRows;
        switch (type) {
            case NOTES:
                affectedRows = db.update(Constants.NOTES_TABLE, values, selection, selectionArgs);
                break;

            case NOTE:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    affectedRows = db.update(Constants.NOTES_TABLE, values, Constants.COLUMN_ID + "=" + id, null);
                } else {
                    affectedRows = db.update(Constants.NOTES_TABLE, values, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
                }
                break;

            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return affectedRows;
    }
}

Manifest file

<manifest package="com.example.android.sqlitedbase"
          xmlns:android="http://schemas.android.com/apk/res/android">
<permission android:name="READ_EXTERNAL_STORAGE"/>
    <permission android:name="WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity android:name=".activities.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".activities.NoteEditorActivity">
        </activity>
        <provider
            android:authorities="com.example.android.sqlitedbase.data.NoteContentProvider"
            android:exported="false"
            android:name=".data.NoteContentProvider" >
        </provider>
    </application>

</manifest>
zek54
  • 415
  • 3
  • 20
  • refer http://stackoverflow.com/questions/29175716/illegalargumentexception-unknown-url-content-content – sasikumar Feb 17 '16 at 09:38
  • @sasikumar I've tried the solution in the link but it doesnt work for my case – zek54 Feb 17 '16 at 09:44
  • Have you checked if you have the table created correctly ? – Spirrow Feb 17 '16 at 09:46
  • Content URI does not seem to be correct, though I am not sure if its just the log or the actual definition. Could you post your Content Provider? –  Feb 17 '16 at 09:49
  • @jvrodrigues I ve put the content provider class – zek54 Feb 17 '16 at 10:01
  • Constants.NOTES_TABLE is this equivalent to "notes"? –  Feb 17 '16 at 10:04
  • @jvrodrigues yes. NOTES_TABLE = "notes"; COLUMN_ID = "_id"; COLUMN_NAME = "name"; COLUMN_TITLE = "title"; COLUMN_CONTENT = "content"; COLUMN_MODIFIED_TIME = "modified_time"; COLUMN_CREATED_TIME = "created_time"; – zek54 Feb 17 '16 at 10:06
  • Ok, so your manifest must be incorrect. –  Feb 17 '16 at 10:14
  • `content:/com.ex...` -> `content://com.ex...` – pskink Feb 17 '16 at 10:31
  • @jvrodrigues Please suggest if any changes required in the recently uploaded manifest file. – zek54 Feb 17 '16 at 10:43
  • you are using wrong `Uri`: `content:/com.ex...` just change it – pskink Feb 17 '16 at 11:10
  • @pskink Initially I was trying with double slash in Uri which caused up IllegalStateException in launcher activity so I referred http://stackoverflow.com/questions/35449271/illegalstateexception-android which solved the problem by using single slash. – zek54 Feb 17 '16 at 11:23
  • read this: http://developer.android.com/intl/es/guide/topics/providers/content-provider-basics.html and this: http://developer.android.com/intl/es/guide/topics/providers/content-provider-creating.html – pskink Feb 17 '16 at 11:25

1 Answers1

0

Your manifest should look like this:

 <provider
      android:authorities="com.example.android.sqlitedbase.data.NoteContentProvider"
      android:name=".data.NoteContentProvider"
      android:exported="false"/>

If it does look like this and still does not work, change the authorities to something along the lines of "com.example.android.provider" on both the manifest and the content provider. The authorities do not have to be the class name of the provider.