我不会在sqlite数据库中保存图像,并在listView中显示已保存图像的ID。单击listitem后,具有所选id的图像应显示在imageView中。节约工作(我想是这样......)。 但点击一个项目后,返回byteArray为空!
在logcat中显示此配药: 无法从具有0行,1列的CursorWindow读取第0行第0列。 无法从CursorWindow读取第0行,第0列。在从中访问数据之前,请确保Cursor已正确初始化。
问题是,byteArray在将其传递给方法后清空。 我怎么能避免这种情况?
databaseManager:
public class DatabaseManager extends SQLiteOpenHelper
{
private static final String tag = "DatabaseManager";
private static final String DB_NAME = "test2.db";
private static final int DB_VERSION = 1;
private static final String TABLENAME = "bilder";
private static final String KEY_ID = "_id";
private static final String KEY_BYTEARRAY = "bild";
private String CREATE_TABLE = "CREATE TABLE "+TABLENAME+" ( "
+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+KEY_BYTEARRAY+" BLOB NOT NULL )";
public DatabaseManager(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(CREATE_TABLE);
}
catch( Exception ex )
{
Log.e( tag, ex.getMessage() );
}
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
{
db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
onCreate(db);
}
public int addByteArray( SQLiteDatabase db, byte[] byteArray )
{
int id = -1;
ContentValues values = new ContentValues();
values.put(KEY_BYTEARRAY, byteArray);
try
{
id = (int) db.insert(TABLENAME, null, values);
}
catch( Exception ex )
{
Log.e( tag, ex.getMessage() );
}
Log.i(tag, "ByteArray has been added to db (id:" + id + ")" );
return id;
}
public int removeByteArray( SQLiteDatabase db, int id )
{
int deletedRows = 0;
try
{
deletedRows = db.delete( TABLENAME, KEY_ID+" = ?", new String[] {(""+id)} );
}
catch( Exception ex )
{
Log.e( tag, ex.getMessage() );
}
return deletedRows;
}
public byte[] getByteArrayWitchId( SQLiteDatabase db, int imageId )
{
byte[] byteArray = null;
try
{
Cursor cursor = db.query(
TABLENAME,
new String[] {KEY_BYTEARRAY},
KEY_ID+" = ?",
new String[] {(""+imageId)},
null, null, null
);
cursor.moveToFirst();
byteArray = cursor.getBlob(0);
cursor.close();
}
catch( Exception ex )
{
Log.e(tag, ex.getMessage());
}
return byteArray;
}
public ArrayList<Integer> getAllId( SQLiteDatabase db )
{
ArrayList<Integer> allIds = new ArrayList<Integer>();
try
{
Cursor cursor = db.query(
TABLENAME,
new String[] {KEY_ID},
null, null,
null, null, null
);
int count = cursor.getCount();
cursor.moveToFirst();
for( int i=0; i<count; i++ )
{
int id=-1;
id = cursor.getInt(0);
allIds.add(id);
cursor.moveToNext();
}
cursor.close();
}
catch( Exception ex )
{
Log.e(tag, ex.getMessage());
}
return allIds;
}
}
MainActivity:
public class MainActivity extends Activity implements OnClickListener, OnItemClickListener
{
private static final int SELECT_PICTURE = 1;
private DatabaseManager databaseManager;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseManager = new DatabaseManager(this);
Button bAddImage = (Button) findViewById( R.id.bAddImage );
bAddImage.setOnClickListener(this);
ListView lvImages = (ListView) findViewById( R.id.lvImages );
lvImages.setOnItemClickListener(this);
loadListViewEntries();
}
@Override
public void onClick(View view)
{
switch( view.getId() )
{
case R.id.bAddImage:
openGalleryToGetImage();
break;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
TextView tv = (TextView) view;
String text = tv.getText().toString();
Toast.makeText(this, "Text:"+text, Toast.LENGTH_SHORT).show();
int imageId = Integer.parseInt(text);
db = databaseManager.getReadableDatabase();
byte[] byteArray = databaseManager.getByteArrayWitchId(db, imageId);
db.close();
if( byteArray != null )
{
Toast.makeText(this, "byteArray != null", Toast.LENGTH_SHORT).show();
// loadByteArrayToImageView(byteArray);
Bitmap image = convertByteArrayToBitmap(byteArray);
ImageView imageView = (ImageView) findViewById( R.id.imageView );
imageView.setImageBitmap(image);
}
else
{
Toast.makeText(this, "byteArray == null", Toast.LENGTH_SHORT).show();
}
}
private void loadListViewEntries()
{
ArrayList<Integer> allIds = new ArrayList<Integer>();
db = databaseManager.getReadableDatabase();
allIds = databaseManager.getAllId(db);
db.close();
if( allIds.size() > 0 )
{
ArrayAdapter<Integer> listAdapter = new ArrayAdapter<Integer>(
this,
android.R.layout.simple_list_item_1 ,
allIds
);
ListView lvImages = (ListView) findViewById( R.id.lvImages );
lvImages.setAdapter(listAdapter);
}
}
private void loadByteArrayToImageView( byte[] bvteArray )
{
Bitmap image = convertByteArrayToBitmap(bvteArray);
ImageView imageView = (ImageView) findViewById( R.id.imageView );
imageView.setImageBitmap(image);
}
private void openGalleryToGetImage()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
public String getPath(Uri uri)
{
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(column_index);
cursor.close();
return imagePath;
}
public byte[] getByteArrayFromUri( Uri uri )
{
String filemanagerstring = uri.getPath();
String selectedImagePath = getPath(uri);
selectedImagePath.getBytes();
String path = selectedImagePath.toString();
Bitmap picture = BitmapFactory.decodeFile(selectedImagePath);
byte[] byteArray = convertBitmapToByteArray(picture);
return byteArray;
}
public byte[] convertBitmapToByteArray( Bitmap bitmap )
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
public Bitmap convertByteArrayToBitmap( byte[] byteArray )
{
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length);
return bitmap;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
byte[] imageBA = getByteArrayFromUri( selectedImageUri );
Bitmap image = convertByteArrayToBitmap(imageBA);
ImageView imageView = (ImageView) findViewById( R.id.imageView );
imageView.setImageBitmap(image);
// loadByteArrayToImageView(imageBA);
db = databaseManager.getWritableDatabase();
int id = databaseManager.addByteArray(db, imageBA);
db.close();
Toast.makeText(this, "new id="+id, Toast.LENGTH_SHORT).show();
loadListViewEntries();
}
}
}
}