我有一个数据库应用程序。它显示包含图像和字符串的项目列表。我想将图像存储在活动中的sqlite数据库中,并从另一个活动中获取它。但它显示插入了空值。这是在数据库中插入图像的代码---
public class EditorActivity extends AppCompatActivity implements View.OnClickListener{
private static int IMAGE_GALLERY_REQUEST=20;
private EditText mNameEditText;
private EditText mDescEditText;
private EditText mResEditText;
private EditText mStatusEditText;
private Button btn;
private byte[] b;
private Bitmap bitmap;
String mName,mDescription,mResident,mStatus;
int data;
public static String EXTRA_DATA="dataNo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
mNameEditText = (EditText) findViewById(R.id.name);
mDescEditText = (EditText) findViewById(R.id.desc);
mResEditText = (EditText) findViewById(R.id.res);
mStatusEditText = (EditText) findViewById(R.id.status);
btn=findViewById(R.id.photo);
btn.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
data = (Integer) (bundle.get(EXTRA_DATA));
}
}
private void saveData()
{
mName=mNameEditText.getText().toString().trim();
mDescription=mDescEditText.getText().toString().trim();
mResident=mResEditText.getText().toString().trim();
mStatus=mStatusEditText.getText().toString().trim();
FriendsDbHelper helper=new FriendsDbHelper(this);
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(FriendContract.FriendEntry.NAME,mName);
values.put(FriendContract.FriendEntry.DESCRIPTION,mDescription);
values.put(FriendContract.FriendEntry.RESIDENCE,mResident);
values.put(FriendContract.FriendEntry.STATUS,mStatus);
values.put(FriendContract.FriendEntry.KEY_IMAGE,b);
db.insert(TABLE_NAME,null,values);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_edit,menu);
return true;
}
public void updateData() {
String name=mNameEditText.getText().toString().trim();
String description=mDescEditText.getText().toString().trim();
String resident=mResEditText.getText().toString().trim();
String status=mStatusEditText.getText().toString().trim();
//try{
FriendsDbHelper helper = new FriendsDbHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
if(TextUtils.isEmpty(name))
{
name=mName;
}
if(TextUtils.isEmpty(description))
{
description=mDescription;
}
if(TextUtils.isEmpty(resident))
{
resident=mResident;
}
if(TextUtils.isEmpty(status))
{
status=mStatus;
}
values.put(NAME, name);
values.put(DESCRIPTION, description);
values.put(RESIDENCE, resident);
values.put(STATUS, status);
db.update(TABLE_NAME, values, _ID + "=?", new String[]{Integer.toString(data)});
/* }
catch (SQLiteException e)
{
Toast.makeText(this,"Update failed",Toast.LENGTH_LONG).show();
}*/
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_save:
saveData();
finish();
return true;
case R.id.action_update:
updateData();
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
Intent photoIntent=new Intent(Intent.ACTION_PICK);
File photoDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String photo=photoDirectory.getPath();
Uri uri=Uri.parse(photo);
photoIntent.setDataAndType(uri,"image/*");
startActivityForResult(photoIntent,IMAGE_GALLERY_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
if(resultCode==IMAGE_GALLERY_REQUEST)
{
Uri uri=data.getData();
InputStream inputStream;
try
{
inputStream=getContentResolver().openInputStream(uri);
bitmap= BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,0,stream);
b=stream.toByteArray();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Unable to open image",Toast.LENGTH_LONG).show();
}
}
}
}
}
因此,每个元素中的字符串都是可见的,但图像不可见。
我从中读到同样的问题 Insert bitmap to sqlite database。他们告诉使用sqlitemaestro软件。如何在android中使用它?
请尽快回复。
答案 0 :(得分:0)
好像是字节数组' b'当您在savedata方法中使用values.put时,请为null,请记录变量并检查。
另外,为了在SQLite中保存图像,我们通常使用二进制大对象,即BLOB
这是一个可以帮助您的资源 How to store(bitmap image) and retrieve image from sqlite database in android?
答案 1 :(得分:0)
使用代码的缩减版本。 SQLite方面似乎没有任何问题。那是使用: -
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static int IMAGE_GALLERY_REQUEST = 20;
private EditText mNameEditText;
private EditText mDescEditText;
private EditText mResEditText;
private EditText mStatusEditText;
private Button btn;
private byte[] b;
private Bitmap bitmap;
String mName, mDescription, mResident, mStatus;
int data;
public static String EXTRA_DATA = "dataNo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameEditText = (EditText) findViewById(R.id.name);
mDescEditText = (EditText) findViewById(R.id.desc);
mResEditText = (EditText) findViewById(R.id.res);
mStatusEditText = (EditText) findViewById(R.id.status);
btn = findViewById(R.id.photo);
btn.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
data = (Integer) (bundle.get(EXTRA_DATA));
}
}
private void saveData() {
mName = mNameEditText.getText().toString().trim();
mDescription = mDescEditText.getText().toString().trim();
mResident = mResEditText.getText().toString().trim();
mStatus = mStatusEditText.getText().toString().trim();
FriendsDbHelper helper = new FriendsDbHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FriendsDbHelper.COL_FRIENDS_NAME, mName);
values.put(FriendsDbHelper.COL_FRIENDS_DESCRIPTION, mDescription);
values.put(FriendsDbHelper.COL_FRIENDS_RESIDENCE, mResident);
values.put(FriendsDbHelper.COL_FRIENDS_STATUS, mStatus);
values.put(FriendsDbHelper.COL_FRIENDS_KEY_IMAGE, b);
db.insert(FriendsDbHelper.TB_FRIENDS, null, values);
}
@Override
public void onClick(View v) {
if (v.getId() == btn.getId()) {
b = new byte[]{0,2,3,4,5,6,7,8,9};
saveData();
}
}
}
按预期保存BLOB,按照: -
onActivityResult
方法中没有相应地设置b,而intent.ACTION_PICK
方法本身依赖于intent.ACTION_PICK
。 您可能希望参考opening an image using Intent.ACTION_PICK(请参阅备注重新归零)和Intent.ACTION_PICK behaves differently或其他许多有关private void saveData()
{
mName=mNameEditText.getText().toString().trim();
mDescription=mDescEditText.getText().toString().trim();
mResident=mResEditText.getText().toString().trim();
mStatus=mStatusEditText.getText().toString().trim();
FriendsDbHelper helper=new FriendsDbHelper(this);
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(FriendContract.FriendEntry.NAME,mName);
values.put(FriendContract.FriendEntry.DESCRIPTION,mDescription);
values.put(FriendContract.FriendEntry.RESIDENCE,mResident);
values.put(FriendContract.FriendEntry.STATUS,mStatus);
values.put(FriendContract.FriendEntry.KEY_IMAGE,b);
//<<<< ADDED to issue Toast if no valid image...
if (b == null) {
Toast.makeText(this,"No valid Image - No Data Stored!",Toast.LENGTH_LONG).show();
return;
}
db.insert(TABLE_NAME,null,values);
}
的SO问题。
您可能还希望使用以下内容: -
$(document).ready(function() {
var cityData = [
{
cityName: 'Bengaluru',
value: 'Bengaluru',
data: [
{
movieName: 'ABC',
theaterName: 'Tulsi Theatre',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: 'DEF',
theaterName: 'PVR',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: 'GHI',
theaterName: 'Srinivasa Theatre',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
],
},
{
cityName: 'Hyderabad',
value: 'Hyderabad',
data: [
{
movieName: '123',
theaterName: 'Theatre1',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: '456',
theaterName: 'PVR2',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: '789',
theaterName: 'Theatre3',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
],
},
{
cityName: 'Guntur',
value: 'Guntur',
data: [
{
movieName: 'ABC1',
theaterName: 'Theatre4',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: 'DEF2',
theaterName: 'PVR3',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
{
movieName: 'GHI3',
theaterName: 'Theatre5',
showTImings:['8:00AM','12:00PM','4:00PM','9:00PM']
},
],
},
{
cityName: 'Ongole',
value: 'Ongole',
data: [],
},
];
var locations = [] ;
$('#selectCity').on('change', function() {
if ($(this).val().indexOf('City') === -1) {
locations = cityData.filter( c => c.cityName === $(this).val(),)[0].data;
var locationString = '';
var locationString2 = '';
if(locations.length == 0){
$('#showTimings').html('No shows available');
}
$.each(locations, function(i, item) {
locationString +='<option value="' +item.theaterName +'">' +item.theaterName +'</option>';
locationString2 +='<option value="' +item.movieName +'">' +item.movieName +'</option>';
$('#showTimings').html('');
$.each(locations[i].showTImings,function(i,v){
var button = $('<button />').html(v);
$('#showTimings').append(button);
});
});
$('#secondselectbox').html(locationString);
$('#thirdselectbox').html(locationString2);
$('span#selectedMovie').text($('#thirdselectbox').val());
$('span#selectedTheater').text($('#secondselectbox').val());
}
});
$('#secondselectbox').on('change', function() {
var theater = $(this).val();
for(var i in locations){
if(locations[i].theaterName===theater){
$('span#selectedTheater').text(theater);
$('span#selectedMovie').text(locations[i].movieName);
$('#thirdselectbox').val(locations[i].movieName);
$('#showTimings').html('');
$.each(locations[i].showTImings,function(i,v){
var button = $('<button />').html(v);
$('#showTimings').append(button);
});
}
}
});
$('#thirdselectbox').on('change', function() {
var movie = $(this).val();
for(var i in locations){
if(locations[i].movieName===movie){
$('span#selectedMovie').text(movie);
$('span#selectedTheater').text(locations[i].theaterName);
$('#secondselectbox').val(locations[i].theaterName);
$('#showTimings').html('');
$.each(locations[i].showTImings,function(i,v){
var button = $('<button />').html(v);
$('#showTimings').append(button);
});
}
}
});
});