这是小弟写的代码,请各位看看:
package com.joyque.image;import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import com.joyque.image.provider.Reader;
import com.joyque.image.provider.Reader.Channel;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.util.Log;public class ReaderProvider extends ContentProvider {private static final String TAG = "ReaderProvider";
private static final String DATABASE_NAME = "reader.db";private static final int DATABASE_VERSION = 1;
private static final String CHANNEL_TABLE_NAME = "channel";private static HashMap<String, String> sChannelProjectionMap;private static final int CHANNEL = 1;
private static final int CHANNEL_ID = 2;private static final UriMatcher sUriMatcher;static final File EXTERNAL_STORAGE_DIRECTORY = Environment
.getExternalStorageDirectory();static final String DATABASE_PATH = "image";private static boolean useLocal = false;private static String dbPath = EXTERNAL_STORAGE_DIRECTORY + File.separator
+ DATABASE_PATH + File.separator + DATABASE_NAME;private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
if (EXTERNAL_STORAGE_DIRECTORY.canWrite()) {
File destination = new File(EXTERNAL_STORAGE_DIRECTORY,
DATABASE_PATH);
if (!destination.exists()) {
if (!destination.mkdirs()) {
useLocal = true;
dbPath = DATABASE_NAME;
}
}
} else {
useLocal = true;
dbPath = DATABASE_NAME;
}
}public void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE " + CHANNEL_TABLE_NAME + " ("
+ Channel._ID + " INTEGER PRIMARY KEY," + Channel._DATA
+ " TEXT," + Channel.CHANNEL_ID + " INTEGER,"
+ Channel.NAME + " TEXT," + Channel.DESCRIPTION + " TEXT,"
+ Channel.IMAGE + " TEXT," + Channel.TIMESTAMP + " TEXT"
+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");db.execSQL("DROP TABLE IF EXISTS channel");
onCreate(db);onCreate(db);
}
}private DatabaseHelper mOpenHelper;@Override
public boolean onCreate() {
mOpenHelper = new DatabaseHelper(getContext());
return true;
}@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {SQLiteQueryBuilder qb = new SQLiteQueryBuilder();switch (sUriMatcher.match(uri)) {
case CHANNEL:
qb.setTables(CHANNEL_TABLE_NAME);
qb.setProjectionMap(sChannelProjectionMap);
break;case CHANNEL_ID:
qb.setTables(CHANNEL_TABLE_NAME);
qb.setProjectionMap(sChannelProjectionMap);
qb.appendWhere(Channel._ID + "=" + uri.getPathSegments().get(1));
break;default:
throw new IllegalArgumentException("Unknown URI " + uri);
}String orderBy = null;
switch (sUriMatcher.match(uri)) {
case CHANNEL:
// If no sort order is specified use the default
if (TextUtils.isEmpty(sortOrder)) {
orderBy = Reader.Channel.DEFAULT_SORT_ORDER;
} else {
orderBy = sortOrder;
}
break;
}SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, orderBy);c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}@Override
public String getType(Uri uri) {
switch (sUriMatcher.match(uri)) {
case CHANNEL:
return Channel.CONTENT_TYPE;case CHANNEL_ID:
return Channel.CONTENT_ITEM_TYPE;default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}@Override
public Uri insert(Uri uri, ContentValues initialValues) {
// Validate the requested uri
switch (sUriMatcher.match(uri)) {
case CHANNEL:
return insertChannel(uri, initialValues);
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}private Uri insertChannel(Uri uri, ContentValues initialValues) {
if (sUriMatcher.match(uri) != CHANNEL) {
throw new IllegalArgumentException("Unknown URI " + uri);
}if (initialValues == null) {
return null;
}ContentValues values = new ContentValues(initialValues);if (values.containsKey(Reader.Channel.CHANNEL_ID) == false) {
values.put(Reader.Channel.CHANNEL_ID, "");
}if (values.containsKey(Reader.Channel.NAME) == false) {
values.put(Reader.Channel.NAME, "");
}if (values.containsKey(Reader.Channel.DESCRIPTION) == false) {
values.put(Reader.Channel.DESCRIPTION, "");
}if (values.containsKey(Reader.Channel.TIMESTAMP) == false) {
values.put(Reader.Channel.TIMESTAMP, "");
}if (values.containsKey(Reader.Channel.IMAGE) == false) {
values.put(Reader.Channel.IMAGE, "");
}SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = db.insert(CHANNEL_TABLE_NAME, Channel.NAME, values);
if (rowId > 0) {
Uri channelUri = ContentUris.withAppendedId(
Reader.Channel.CONTENT_URI, rowId);getContext().getContentResolver().notifyChange(channelUri, null);return channelUri;
}
throw new SQLException("Failed to insert row into " + uri);
}/**
* 对数据表channel,catalog,block,book,chapter的删除操作
*/
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
switch (sUriMatcher.match(uri)) {
case CHANNEL:
count = db.delete(CHANNEL_TABLE_NAME, where, whereArgs);
break;case CHANNEL_ID:
String channelId = uri.getPathSegments().get(1);
count = db.delete(CHANNEL_TABLE_NAME,
Channel._ID
+ "="
+ channelId
+ (!TextUtils.isEmpty(where) ? " AND (" + where
+ ')' : ""), whereArgs);
break;default:
throw new IllegalArgumentException("Unknown URI " + uri);
}getContext().getContentResolver().notifyChange(uri, null);
return count;
}/**
* 对数据表channel,catalog,block,book,chapter的更新操作.
*/
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
switch (sUriMatcher.match(uri)) {
case CHANNEL:
count = db.update(CHANNEL_TABLE_NAME, values, where, whereArgs);
break;case CHANNEL_ID:
String channelId = uri.getPathSegments().get(1);
count = db.update(CHANNEL_TABLE_NAME, values,
Channel._ID
+ "="
+ channelId
+ (!TextUtils.isEmpty(where) ? " AND (" + where
+ ')' : ""), whereArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}getContext().getContentResolver().notifyChange(uri, null);
return count;
}public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
if (sUriMatcher.match(uri) != CHANNEL_ID)
throw new IllegalArgumentException(
"openFile not supported for directories");
try {
String id = uri.getPathSegments().get(1);
ContentValues values = new ContentValues();
values.put(Channel._DATA, EXTERNAL_STORAGE_DIRECTORY
+ File.separator + DATABASE_PATH + File.separator 
+ "1.jpg");update(uri, values, new String(Channel._ID + "=" + id), null);
return openFileHelper(uri, mode);
} catch (FileNotFoundException e) {
throw new FileNotFoundException();
}
}// public ParcelFileDescriptor openFile(Uri uri, String mode)
// throws FileNotFoundException {
//
// if (sUriMatcher.match(uri) != CHANNEL_ID)
// throw new IllegalArgumentException(
// "openFile not supported for directories");
// try {
// return openFile(uri, mode);
// } catch (FileNotFoundException e) {
// throw new FileNotFoundException();
// }
//
// }// public ParcelFileDescriptor openFile(Uri uri, String mode) throws
// FileNotFoundException {
//  
// if (sUriMatcher.match(uri) != CHANNEL_ID) throw new
// IllegalArgumentException("openFile not supported for directories");
// try {
// String id = uri.getPathSegments().get(1);
// ContentValues values = new ContentValues();
// values.put(Reader.Channel._DATA, uri.toString() + File.separator +
// c.getString(0));
// update(uri,values,new String(Reader.Channel._ID+"="+id),null);
// return openFile(uri, mode);
// } catch (FileNotFoundException e) { throw new FileNotFoundException(); }
//  
// }static {
// -- URI匹配器和相应的数据表相匹配.
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);sUriMatcher.addURI(Reader.AUTHORITY, "channel", CHANNEL);
sUriMatcher.addURI(Reader.AUTHORITY, "channel/#", CHANNEL_ID);// -- 创建相应表列名与相应JavaBean 映射.
sChannelProjectionMap = new HashMap<String, String>();
sChannelProjectionMap.put(Channel._ID, Channel._ID);
sChannelProjectionMap.put(Channel.CHANNEL_ID, Channel.CHANNEL_ID);
sChannelProjectionMap.put(Channel.NAME, Channel.NAME);
sChannelProjectionMap.put(Channel.DESCRIPTION, Channel.DESCRIPTION);
sChannelProjectionMap.put(Channel.TIMESTAMP, Channel.TIMESTAMP);
sChannelProjectionMap.put(Channel.IMAGE, Channel.IMAGE);
sChannelProjectionMap.put(Channel._DATA, Channel._DATA);
}
}
我的其它直都插入了,就是图片的文件夹路径不会插入,请指教。也不你写的调用端的代码在哪个位置取出图片,自己写的openFile方法貌似插入了图片路径,但是进入Sqlite3数据表一查,什么也没有插入。 

解决方案 »

  1.   

    update(uri,   values,   new   String(Channel._ID   +   "= "   +   id),   null); 这句里面"= "后面的空格去掉,直接是"=".
      

  2.   

    这个不是原因,我的代码本来就没有空格。
    update(uri, values, new String(Channel._ID + "= " + id), null);  
    不知怎么回事,就出了空格,我个人认为问题出在openFile()方法的重写肯定错了,我也不知道如何改,就发帖求救!!