从sqlite存储和检索Uri [英] Storing and Retrieving Uri from sqlite

查看:73
本文介绍了从sqlite存储和检索Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手开发人员,目前正在开发一个应用程序,该程序的部分功能允许用户捕获图像,将其存储在应用程序的文件系统中以及将其引用存储在SQLite数据库的列中.然后,用户将能够基于网格视图中与数据库中关联的任何标准在网格视图中查看这些图像(例如,仅显示某种颜色的图像).

I’m a newbie developer and I am currently working on an app in which part of the functionality allows users to capture an image, store it in the app’s file system, and store its reference in a column of a SQLite database. The user will then be able to view these images in a gridview based on whatever criteria it is associated with in the database (e.g. only displaying images that are a certain colour).

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

At first I actually "grabbed" the captured image’s file name and stored that in the database, using this code:

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

    }

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

However I realized that the filename stored is just an ordinary string in the context of the app and does not actually point to any one image stored in the file system.

我想知道的是:

  1. 如何在我的SQLite数据库中存储图像的Uri 被捕获
  2. 如何根据存储的图像检索图像显示在表格视图中的Uri.

欢迎提出任何建议,示例代码,反馈.

Any suggestions, sample code, feedback is welcomed.

推荐答案

在我的应用程序中,我要将用户的个人资料图片存储在SQLite数据库中,因此我添加了一个用于存储图像路径的字符串列,图像将从库中选择

In my application I want to store profile picture of user in SQLite database So I have added one String Column for Storing Path of Image,image will be selected from Gallery

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

然后在显示中我已经使用光标获取了图像的路径

Then in for displaying I have fetch path of image using Cursor

String employeeimage;
...
employeeimage = cursor.getString(cursor.getColumnIndex("employeeimage"));  //employeeimage is column name

现在,我已在RecyclerView中显示所有数据,因此我已使用适配器绑定数据.

Now I have display all data in RecyclerView so I have use Adapter for binding data.

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

这篇关于从sqlite存储和检索Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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