我想知道如何获取存储在 eclipse 中可绘制文件夹中的图像的 imagePath。imagePath 将存储在 SQLite 数据库中。所以现在我想知道如何在数据库中检索 imagePath 的 Uri。任何帮助将不胜感激。谢谢你。
ImagePage.java
package main.page;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImagesPage extends Activity
{
Integer[] imageIDs = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};
ImageAdapter imgDB = new ImageAdapter(this);
ArrayList<String> imageNames = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
Button btnNext = (Button)findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent i = new Intent(ImagesPage.this, ImagesSecondpage.class);
startActivity(i);
}
});
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImgAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
imgDB.open();
long _id = imgDB.insertImage("pic" + (position + 1)+ ".jpg");
imgDB.close();
}
});
}
public class ImgAdapter extends BaseAdapter
{
private Context context;
public ImgAdapter(Context c)
{
context = c;
}
@Override
public int getCount()
{
return imageIDs.length;
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView ,ViewGroup parent)
{
ImageView imageView;
if(convertView == null)
{
imageView = new ImageView(context);
imageView.setImageResource(getResources().getIdentifier(imageNames.get(position), defType, defPackage))
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
}else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}
ImageAdapter.java
package main.page;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ImageAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "img_name";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "anniversary";
private static final String DATABASE_TABLE = "image";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table image (_id integer primary key autoincrement, "+ "img_name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public ImageAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}catch(SQLException e)
{
e.printStackTrace();
}
}
@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 image");
onCreate(db);
}
}
public ImageAdapter open() throws SQLException
{
db = DBHelper.getReadableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertImage(String img_name)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, img_name);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteImage(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +"="+ rowId, null) > 0;
}
public Cursor getAllImages()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, null, null, null, null, null);
}
public Cursor getImage(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, KEY_ROWID +"="+ rowId, null, null, null, null, null);
if(mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateImage(long rowId, String img_name)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, img_name);
return db.update(DATABASE_TABLE, args, KEY_ROWID +"="+ rowId, null) > 0;
}
}
我再次更新了 ImagePage.java 的编码。