android sqlite 存图片示例
时间: 2023-07-22 21:40:17 浏览: 82
以下是一个完整的 Android SQLite 存储图片的示例代码:
1. 在 layout 目录下创建一个名为 activity_main.xml 的布局文件,添加一个 ImageView 和一个 Button:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/placeholder" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Choose Image" />
</RelativeLayout>
```
2. 在 MainActivity.java 中实现图片的存储和显示逻辑:
```java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button button;
private Bitmap selectedBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
try {
selectedBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
imageView.setImageBitmap(selectedBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void saveImage(View view) {
if (selectedBitmap != null) {
SQLiteDatabase db = openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Images (Name VARCHAR, Data BLOB)");
ContentValues values = new ContentValues();
values.put("Name", "test");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
values.put("Data", byteArray);
db.insert("Images", null, values);
db.close();
Toast.makeText(this, "Image saved to database", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
}
}
public void loadImage(View view) {
SQLiteDatabase db = openOrCreateDatabase("MyDatabase", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM Images WHERE Name='test'", null);
if (cursor.moveToFirst()) {
byte[] byteArray = cursor.getBlob(1);
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView.setImageBitmap(bitmap);
} else {
Toast.makeText(this, "Image not found in database", Toast.LENGTH_SHORT).show();
}
cursor.close();
db.close();
}
}
```
3. 在 AndroidManifest.xml 中添加读取外部存储权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
在运行应用程序时,点击 Choose Image 按钮可以选择图片并在 ImageView 中显示。点击 Save Image 按钮将所选图片存储到 SQLite 数据库中,点击 Load Image 按钮从数据库中读取图片并在 ImageView 中显示。
阅读全文