首页 > 解决方案 > 在 Android 上的 SQLite 数据库中实现相机图像存储的问题

问题描述

我最近制作了一个具有基本 Intent 的应用程序,可以在设备上打开默认的相机应用程序。但我想扩展它的功能,使其能够将捕获的图像从相机存储到 SQLite 数据库。连同显示已经存储在数据库中的图像。我还想显示在图像下方捕获的图像的日期和时间,以及在图像的日期/时间下方添加评论框。

MainActivity 的代码如下:

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnCamera = (Button) findViewById(R.id.btnCamera);
        imageView = (ImageView) findViewById(R.id.imageView);

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bitmap = (Bitmap)data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);
    }
}

Activity 的 Layout XML 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:orientation="vertical"
    android:weightSum="10"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_weight="9"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        />

    <Button
        android:id="@+id/btnCamera"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/white"
        android:text="Open Camera"
        />

</LinearLayout>

标签: androidsqliteandroid-image

解决方案


推荐阅读