如何在手机图库中添加Sqlite数据库中的图像?

时间:2017-11-27 09:39:01

标签: java android database sqlite

这是我从图库中获取数据的代码,并将其保存在SQLite数据库中,但它无法正常工作

    .....

    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 Contacts
            Log.d("Insert: ", "Inserting ..");
            db.addContact(new Contact(resultCode, "Android", imageInByte));
            Intent i = new Intent(Editor.this, Editor.class);
            startActivity(i);
            finish();
        }
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

存储图像的Uri,通过Uri,您可以在ImageView中设置图像

   Uri imageUri;

   Intent pickUpImage = new Intent(Intent.ACTION_PICK);
            pickUpImage.setType("image/*");
            startActivityForResult(pickUpImage, 1);

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            imageUri = uri;
            profilePic.setImageURI(imageUri);

        }
    }