2

I have have not found a practical example that specifically pertains to saving the file path of a image that you just took with the camera app to a SQLite database in your application.

I have seen code to save an image from a HTML source... no good! My issue is that I have the URI but honestly, I can't figure out with the available data (dev guide, Stack Overflow questions) how to insert that file path to my database column.

Here is my code where I try setting an edit text field so that the path is savable to the database. I tried this in the emulator:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Bitmap x = (Bitmap) data.getExtras().get("data");
        File storagePath = new File(Environment.getExternalStorageDirectory() +
                                    "/DCIM/GPAA/"); 
        storagePath.mkdirs();
        File myImage = new File(storagePath,
                                System.currentTimeMillis() + ".jpg");
        try { 
            FileOutputStream out = new FileOutputStream(myImage); 
            x.compress(Bitmap.CompressFormat.JPEG, 80, out); 
            Uri outputFileUri = Uri.fromFile(myImage);
            mnotesText = (EditText)findViewById(R.id.notes);
            mnotesText.setText (outputFileUri.toString());
            ((ImageView)findViewById(R.id.photoResultView)).setImageBitmap(x);
            out.close();

            Toast.makeText(Review.this, 
                           "Image saved: " + outputFileUri.toString(), 
                           Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
        } catch (IOException e){
        }
    }
}

With this code the toast verifies that the string is available and correct. However the entry to the mnotesText.setText (outputFileUri.toString()); works in the emulator. But strangely enough will not work on the phone.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
Mr. Ed
  • 74
  • 1
  • 4
  • 17
  • 1
    If your problem is solved, you should post the solution as an answer (yes, it's perfectly acceptable to answer your own question). If you'd like to wait for a better answer, just don't accept it. (You'll note that thousands of questions have been solved here, but very few of them have "SOLVED" in the title.) – Keith Thompson Dec 19 '11 at 01:57

1 Answers1

1

Mr. Ed answered provided this answer in his own question, I cleaned it up below:

I'm sure this is not the preferred way to do this but it works:

  1. In your layout.xml define a edit text field and hide the text by changing the color of the text to the background. Or not if you want to see the text.

  2. Add the appropriate field in your database manager.

  3. In your activity, insert the text to the text field.

    String path = outputFileUri.toString();
    mpic.setText (path);
    

When it is saved the path is saved to the database. Use BitmapFactory to decode the image path like this:

// String username created from edit text field to a string
String username = mpic.getText().toString(); 
// Bitmap will decode the string to a image (bitmap)
Bitmap myBitmap = BitmapFactory.decodeFile(username);
// Image view used to set the bitmap
ImageView myImage = (ImageView) findViewById(R.id.photoResultView);
// Setting the image to the image view
myImage.setImageBitmap(myBitmap);
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229