繁体   English   中英

如何从电话画廊在Sqlite数据库中添加图像?

[英]How to add image in Sqlite database from phone gallery?

这是我的代码,用于从库中获取数据并将其保存在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;
    }
}

存储图像的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);

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM