2013-11-28 52 views
2

我是一名新手开发人员,我目前正在开发一个应用程序,其中部分功能允许用户捕获图像,将其存储在应用程序的文件系统中,并将其引用存储在SQLite数据库的列。然后,用户将能够基于它在数据库中关联的任何标准(例如仅显示具有某种颜色的图像)在gridview中查看这些图像。从sqlite存储和检索Uri

起初,我实际上是“抓住”所拍摄的图像的文件名,并存储在数据库中,使用此代码:

//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数据库开放的,因为它 被捕获
  2. 我怎么会去检索你的图像基础在他们存储的 Uri显示在一个gridview。

欢迎任何建议,示例代码,反馈意见。

+1

就像你提到的那样,URI实际上只是一个具有特定格式的文本。你可以将它存储为'String'(或SQLite中的'TEXT')。为了从'String'获得相同的URI,你可以使用'Uri.parse()' –

+0

@AndrewT。感谢您的快速回复和建议。 – ImNewHere

+0

@AndrewT。如果它只是一个文本,我可以使用我已经存储在我的数据库中的文件名,并使用它来检索gridview中的图像? – ImNewHere

回答

0

在我的应用程序要存储的用户在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); 
}