Storing and retrieving Uri from sqlite

I am a beginner developer, and I am currently working on an application in which part of the functions allows users to capture an image, store it in the application file system and store its link in the SQLite database column. Then the user will be able to view these images in a gridview based on any criteria with which he is associated in the database (for example, only displaying images of a certain color).

First, I actually “grabbed” the captured image file name and saved it to 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 stored file name is a regular string in the application context and does not actually indicate any image stored in the file system.

What I would like to know:

  • Uri SQLite,
  • Uri .

, , .

+2
1

SQLite. , String ,

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

Source: https://habr.com/ru/post/1527659/


All Articles