Sqlite Retrieving image by path

2019-09-02 02:59发布

问题:

Please can any one help !

There is any other ways to save and retrieve the image ? but sure i don't need to store image in sqlite. i need to store the file name in sqlite. retrieve by path

capturing image private void captureImage() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);



    }

and also i have created the directory in sd card to save my captured images via my application below

private static File getOutputMediaFile(int type)
                  {

        // External sdcard location

        File mediaStorageDir = new File
                (
                Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME
                );

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) 
        {
        if (!mediaStorageDir.mkdirs()) 
        {
        Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
            + IMAGE_DIRECTORY_NAME + " directory");
        return null;
        }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;

        if (type ==MEDIA_TYPE_IMAGE)
        {
            mediaFile = new File (mediaStorageDir.getPath()+ File.separator + "IMG_" + timeStamp + ".jpg");
        }
        else 
        {
            return null;
        }
    // TODO Auto-generated method stub
        return mediaFile;
                }

回答1:

Override the OnActivityResult function like this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK)
    {
        // call your database helper class.fetch the imagepath.and store 
        // it into a static global string variable.
    }
}