【问题标题】:Saving image file path to sqlite database将图像文件路径保存到 sqlite 数据库
【发布时间】:2012-01-18 17:58:16
【问题描述】:

我还没有找到一个实际示例,专门用于将您刚刚使用相机应用程序拍摄的图像的文件路径保存到应用程序中的 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()); 的条目在模拟器中有效。但奇怪的是不能在手机上工作。

【问题讨论】:

  • 如果您的问题得到解决,您应该将解决方案作为答案发布(是的,回答您自己的问题是完全可以接受的)。如果您想等待更好的答案,请不要接受。 (您会注意到这里已经解决了数千个问题,但其中很少有标题中包含“已解决”的问题。)

标签: java android image sqlite path


【解决方案1】:

先生。 Ed在他自己的问题中回答了这个答案,我在下面清理了它:

我确定这不是执行此操作的首选方式,但它确实有效:

  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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2019-04-02
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2017-09-25
    • 2021-06-05
    相关资源
    最近更新 更多