我是Android编程的新手,所以希望有人可以帮忙。尝试将图库或相机中的图像添加到我的SQLite数据库时出现问题。我偶然发现了一个拥有CameraGallerySqliteDemo的GitHub Found here
我尝试根据自己的需要修改代码,但无法将图像添加到gallery
表中。
以下是我用来将图像添加到数据库的类。
public class add_gallery extends AppCompatActivity {
Button addImage;
ArrayList<Gall> imageArry = new ArrayList<Gall>();
GalleryImageAdapter imageAdapter;
private static final int CAMERA_REQUEST = 1;
private static final int PICK_FROM_GALLERY = 2;
ListView dataList;
//maybe change to image title later
byte[] imageName;
int imageId;
Bitmap theImage;
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_gallery);
dataList = (ListView) findViewById(R.id.list);
/**
* create DatabaseHelper object
*/
myDb = new DatabaseHelper(this);
/**
* Reading and getting all records from database
*/
List<Gall> images = myDb.getAllGallery();
for (Gall cn : images) {
String log = "ID:" + cn.getID() + " Image: " + cn.getImage()
+ " ,Title: " + cn.getTitle()
+ " ,Caption: " + cn.getCaption();
// Writing Galls to log
Log.d("Result: ", log);
// add images data in arrayList
imageArry.add(cn);
}
/**
* Set Data base Item into listview
*/
imageAdapter = new GalleryImageAdapter(this, R.layout.gallery_list,
imageArry);
dataList.setAdapter(imageAdapter);
/**
* go to next activity for detail image
*/
dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
imageName = imageArry.get(position).getImage();
imageId = imageArry.get(position).getID();
Log.d("Before Send:****", imageName + "-" + imageId);
// convert byte to bitmap
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imageName);
theImage = BitmapFactory.decodeStream(imageStream);
Intent intent = new Intent(add_gallery.this,
DisplayImageActivity.class);
intent.putExtra("imageid", imageId);
intent.putExtra("imagename", theImage);
startActivity(intent);
}
});
/**
* open dialog for choose camera/gallery
*/
final String[] option = new String[] { "Take from Camera",
"Select from Gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Option");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e("Selected Item", String.valueOf(which));
if (which == 0) {
callCamera();
}
if (which == 1) {
callGallery();
}
}
});
final AlertDialog dialog = builder.create();
addImage = (Button) findViewById(R.id.btnAdd);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
/**
* On activity result
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case CAMERA_REQUEST:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap yourImage = extras.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Galls
Log.d("Insert: ", "Inserting ..");
myDb.addGallery(new Gall("Android", imageInByte));
Intent i = new Intent(add_gallery.this,
add_gallery.class);
startActivity(i);
finish();
}
break;
case PICK_FROM_GALLERY:
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap yourImage = extras2.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Galls
Log.d("Insert: ", "Inserting ..");
myDb.addGallery(new Gall("Android", imageInByte));
Intent i = new Intent(add_gallery.this,
add_gallery.class);
startActivity(i);
finish();
}
break;
}
}
/**
* open camera method
*/
public void callCamera() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("aspectX", 0);
cameraIntent.putExtra("aspectY", 0);
cameraIntent.putExtra("outputX", 200);
cameraIntent.putExtra("outputY", 150);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
/**
* open gallery method
*/
public void callGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
}
以下是我调用图片添加到图库的方法addGallery
public// Adding new image to gallery
void addGallery(Gall gallery) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(IMAGE, gallery._image);
values.put(TITLE, gallery._title);
values.put(CAPTION, gallery._caption);
// Inserting Row
db.insert(GALLERY_TABLE, null, values);
db.close(); // Closing database connection
}
以下是Gall
类
public class Gall {
// private variables
int _id;
byte[] _image;
String _title;
String _caption;
// Empty constructor
public Gall() {
}
// constructor
public Gall(int keyId, byte[] image, String title, String caption) {
this._id = keyId;
this._image = image;
this._title = title;
this._caption = caption;
}
public Gall(byte[] image, String title, String caption) {
this._image = image;
this._title = title;
this._caption = caption;
}
public Gall(int keyId) {
this._id = keyId;
}
// getting ID
public int getID() {
return this._id;
}
// setting id
public void setID(int keyId) {
this._id = keyId;
}
// getting image
public byte[] getImage() {
return this._image;
}
// setting image
public void setImage(byte[] image) {
this._image = image;
}
// getting
public String getTitle() {
return this._title;
}
// setting title
public void setTitle(String title) {
this._title = title;
}
// getting caption
public String getCaption() {
return this._title;
}
// setting caption
public void setCaption(String caption) {
this._title = caption;
}
也许训练有素的眼睛可以发现我出错的地方,我没有收到任何错误,只是图片没有被添加到数据库表中。任何帮助将不胜感激。
答案 0 :(得分:0)
将图像作为字节数组存储在db中。 请查看示例here。这可能会对你有所帮助。
答案 1 :(得分:0)
您应该将图像保存在文件夹中,并将其路径保存在db中。如果您希望图像受到保护并且不想在图库中显示它们,那么有一些方法可以使用.nomedia文件,或者您可以将图像保存在私人文件夹中,而不是您的任何其他应用程序都无法访问