2011-12-12 514 views
2

我還沒有找到一個實際的例子,具體涉及到保存圖像的文件路徑,你剛剛拿着相機應用程序到應用程序中的SQLite數據庫。保存圖像文件路徑到sqlite數據庫

我看到了代碼來保存從HTML源代碼的圖像...不行!我的問題是我有URI,但老實說,我不知道可用的數據(開發指南,堆棧溢出問題)如何將該文件路徑插入到我的數據庫列。

這裏是我的代碼,我嘗試設置編輯文本字段,以便路徑可以保存到數據庫。我在模擬器中試過這個:

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

用這段代碼,toast驗證字符串是否可用並且正確。然而,mnotesText.setText (outputFileUri.toString());的條目在仿真器中起作用。但奇怪的是,在手機上無法使用。埃德

+1

如果你的問題解決了,你應該張貼的溶液,作爲一個答案(是的,這是完全可以接受的回答你自己的問題)。如果您想等待更好的答案,只是不接受它。 (您會注意到這裏已經解決了數千個問題,但是標題中只有很少的問題已經解決了。) –

回答

1

先生回答了自己的問題提供了這個答案,我清理了如下:

我敢肯定這是不是這樣做的首選方式,但它的工作原理:

  1. 在您的layout.xml中定義一個編輯文本字段,並通過將文本的顏色更改爲背景來隱藏文本。或者,如果你想看到文本。

  2. 在您的數據庫管理器中添加相應的字段。

  3. 在您的活動中,將文本插入文本字段。

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

當保存路徑保存到數據庫中。使用BitmapFactory到圖像解碼路徑是這樣的:

// 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); 
相關問題