如何在手机图库中添加Sqlite数据库中的图像? [英] How to add image in Sqlite database from phone gallery?

查看:149
本文介绍了如何在手机图库中添加Sqlite数据库中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my code to get data from gallery and save it in SQLite database but its not working

    .....

    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);

        }
    }

这篇关于如何在手机图库中添加Sqlite数据库中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆