在我的应用程序中,我想将用户的个人资料图片存储在 SQLite 数据库中,因此我添加了一个字符串列来存储图像路径,图像将从图库中选择
//Executed When user Click on image for selecting profile from Gallery
ImageView profileperson = (ImageView) findViewById(R.id.profileperson);
profileperson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
String path;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] fillPath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, fillPath, null, null, null);
assert cursor != null;
cursor.moveToFirst();
path = cursor.getString(cursor.getColumnIndex(fillPath[0]));
cursor.close();
profileperson.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
然后在显示中我使用光标获取图像路径
String employeeimage;
...
employeeimage = cursor.getString(cursor.getColumnIndex("employeeimage")); //employeeimage is column name
现在我已经在 RecyclerView 中显示所有数据,因此我使用 Adapter 来绑定数据。
List<Data> list = Collections.emptyList();
...
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
/*
* get items from list of particular position and set into respective textview
* */
String path=list.get(position).employeeImage;
Picasso.with(context).load(new File(path)).into(holder.imageViewPerson);
}