gpt4 book ai didi

android - 从 sqlite 存储和检索 Uri

转载 作者:太空狗 更新时间:2023-10-29 15:07:45 25 4
gpt4 key购买 nike

我是一名开发新手,目前正在开发一款应用,其中部分功能允许用户捕获图像,将其存储在应用的文件系统中,并将其引用存储在 SQLite 数据库的列中。然后,用户将能够根据与数据库中关联的任何标准(例如,仅显示特定颜色的图像)在 GridView 中查看这些图像。

起初我实际上“抓取”了捕获图像的文件名并将其存储在数据库中,使用以下代码:

//Initializing Variables:
protected ImageButton _button;
protected ImageView _image;
protected TextView _field;
protected String _path;
protected String name;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";

@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.newitemui);

//Creating the "item_images" directory and File:
File dir = new File("sdcard/item_images");
try
{
//Create new directory:
if(dir.mkdir())
{
System.out.println("Directory created");
}
else
{
System.out.println("Directory is not created");
}
}
catch(Exception e)
{
e.printStackTrace();
}

//Setting the current time stamp for image name:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());

_image = ( ImageView ) findViewById( R.id.image );
_field = ( TextView ) findViewById( R.id.field );
_button = ( ImageButton ) findViewById( R.id.button );
_button.setOnClickListener( new ButtonClickHandler() );
name = "IMG_" + timeStamp + ".png";
_path = Environment.getExternalStorageDirectory() + File.separator + "/item_images" + "/" + name;

Toast toast = Toast.makeText(NewItemUI.this,"Touch Camera to Capture Image", Toast.LENGTH_LONG);
toast.show();
toast.setGravity(Gravity.DISPLAY_CLIP_HORIZONTAL|Gravity.BOTTOM, 0, 200);
}

public class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
startCameraActivity();
}
}

protected void startCameraActivity()
{
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

startActivityForResult( intent, 0 );
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch( resultCode )
{
//If user did not take photo, return to the original screen with no result:
case 0:
break;

//If user took a photo, insert the image name into database and return to the original screen with the captured image displayed:
case -1:

AppDatabase appdb = new AppDatabase(this);
SQLiteDatabase sql = appdb.getWritableDatabase();
sql.execSQL("INSERT INTO tblClothingItem (imagePath) VALUES ('"+name+"');");
sql.close();
appdb.close();

onPhotoTaken();
break;
}

}

但是我意识到存储的文件名只是应用程序上下文中的一个普通字符串,实际上并不指向文件系统中存储的任何图像。

我想知道的是:

  1. 如何在我的 SQLite 数据库中存储图像的 URI被俘虏
  2. 我将如何根据存储的图像检索这些图像在 GridView 中显示的 Uri。

欢迎任何建议、示例代码和反馈。

最佳答案

在我的应用程序中,我想在 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 来绑定(bind)数据。

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

关于android - 从 sqlite 存储和检索 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com