从手机相册中获取图像并将其存储在SQLite数据库中。

huangapple go评论45阅读模式
英文:

android take image from phone album and store it in sqlite database

问题

现在你可以尝试将选定的图像存储到SQLite数据库中。以下是继续的步骤:

  1. 创建一个SQLite数据库,如果尚未创建。

  2. 在数据库中创建一个表,用于存储图像。表可以包含至少两个列,一个用于图像的唯一标识(通常是自增ID),另一个用于存储图像的二进制数据。

  3. 将选定的图像转换为字节数组,以便将其存储在数据库中。

  4. 使用合适的SQL语句将图像数据插入到数据库表中。

这是一个示例方法,将选定的图像存储到SQLite数据库中:

public void storeImageInDatabase(Bitmap image) {
    try {
        // Convert the Bitmap image to a byte array
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] imageBytes = byteArrayOutputStream.toByteArray();

        // Open or create the SQLite database
        SQLiteDatabase db = openOrCreateDatabase("YourDatabaseName", Context.MODE_PRIVATE, null);

        // Create a table if it doesn't exist (You should do this only once)
        db.execSQL("CREATE TABLE IF NOT EXISTS Images (id INTEGER PRIMARY KEY, image_data BLOB)");

        // Insert the image data into the database
        ContentValues values = new ContentValues();
        values.put("image_data", imageBytes);
        db.insert("Images", null, values);

        // Close the database
        db.close();

        // Optionally, you can show a message to indicate successful storage.
        // Toast.makeText(this, "Image stored in the database", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

在上述代码中,你需要替换YourDatabaseName为你的数据库名称,并根据你的应用需求对数据库表结构进行调整。成功执行此方法后,选定的图像将存储在数据库中。

英文:

i'm making an app where the user can choose images from the phone's album and store it into a table in sqlite database, i have seen many posts here about this issue but couldn't understand the solutions (neither did they work for me), in my activity i have an imageview which when clicked will open the album and allow the user to choose an image and also i have a button which when clicked will store the image in sqlite database, i'm just able to choose the image but after that i'm stuck, i got to this method in my code:

public void getImage(View view) throws IOException {
        ImageView v=(ImageView)view;
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent,1);
    }


    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            }
        }
    }

now what should i do after this to store the image in the db?

答案1

得分: 1

如果您确定图像不会从内部存储中删除,您可以保存图像路径。如果想要保存图像数据,可以按照以下方式进行操作。

选择图像的部分:

private void selectImage() {
     Intent intent = new Intent();
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     startActivityForResult(intent, IMAGE_REQ);
}

onActivityResult中,从意图数据中获取...

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQ && resultCode == Activity.RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
                Bitmap.createScaledBitmap(bitmap, 150, 150, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

从位图获取字节数组:

public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
        if(bitmap == null) return null;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);

        return byteArrayOutputStream.toByteArray();
    }

然后将字节数组保存为SQLite中的 blob。

获取图像作为位图:

public static Bitmap getBitmapFromByteArray(byte[] blob){
        if(blob == null) return null;

        Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
        return bitmap;
    }
英文:

If you are sure about the images will not be removed from internal storage, you can save the image path. If want to save the image data, you can do the following.

for selecting image

private void selectImage() {
     Intent intent = new Intent();
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     startActivityForResult(intent, IMAGE_REQ);
}

In onActivityResult, from Intent data ...

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQ && resultCode == Activity.RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
                Bitmap.createScaledBitmap(bitmap, 150, 150, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

get byte[] from bitmap

public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
        if(bitmap == null) return null;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);

        return byteArrayOutputStream.toByteArray();
    }

then save the byte[] as blob in sqlite

for getting the images as bitmap

public static Bitmap getBitmapFromByteArray(byte[] blob){
        if(blob == null) return null;

        Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
        return bitmap;
    }

huangapple
  • 本文由 发表于 2020年8月3日 04:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定