我如何将图像从 phone 内存上传到 android sqlite 数据库
how can i upload image from phone memory to android sqlite database
我正在尝试开发一个 android 应用程序,其中有一个选项可以从 phone 内存上传图像,然后它将显示在列表视图中,其中包含用户给出的一些标题。
我已经创建了它的布局,但我不知道如何实现这个概念。
我在 LinearLayout 中使用一个 EditText 和一个 Button 上传文件,在另一个 activity 中使用 ListView显示图片
首先你必须从Phone内存中获取图像,例如`
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Bitmap mBitmap = null;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri chosenImageUri = data.getData();
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
}
}
` 您将获得位图图像形式的图像。将此位图图像转换为 ByteArray[]:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap .compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
然后在数据库中取一个数据类型为 blob 的列,然后将这个字节数组插入该列;
db.execSQL("create table Images(id integer, imageData blob);");
用于插入
ContentValues values = new ContentValues();
values.put("id", imageIdValue);
values.put("imageData ", byteArray);
database.insert("Image", null, values);
希望对您有所帮助。
我正在尝试开发一个 android 应用程序,其中有一个选项可以从 phone 内存上传图像,然后它将显示在列表视图中,其中包含用户给出的一些标题。
我已经创建了它的布局,但我不知道如何实现这个概念。
我在 LinearLayout 中使用一个 EditText 和一个 Button 上传文件,在另一个 activity 中使用 ListView显示图片
首先你必须从Phone内存中获取图像,例如`
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Bitmap mBitmap = null;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri chosenImageUri = data.getData();
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
}
}
` 您将获得位图图像形式的图像。将此位图图像转换为 ByteArray[]:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap .compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
然后在数据库中取一个数据类型为 blob 的列,然后将这个字节数组插入该列;
db.execSQL("create table Images(id integer, imageData blob);");
用于插入
ContentValues values = new ContentValues();
values.put("id", imageIdValue);
values.put("imageData ", byteArray);
database.insert("Image", null, values);
希望对您有所帮助。