保存图像文件路径SQLite数据库 [英] Saving image file path to sqlite database

查看:403
本文介绍了保存图像文件路径SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有发现特别有助节省您刚才拿着相机应用SQLite数据库在应用程序中的图像的文件路径一个实际的例子。

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.

我见过code从HTML源代码保存图像...没有好!我的问题是,我有URI但说实话,我想不通的可用数据(开发指南,堆栈溢出的问题)如何插入文件的路径到我的数据库列。

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.

下面是我的code,我尝试设置编辑文本字段,以便路径是可保存到数据库。我在模拟器尝试这样的:

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

有了这个code敬酒验证字符串是可用的和正确的。然而,进入到 mnotesText.setText(outputFileUri.toString()); 作品在模拟器。但奇怪的是不会在手机上运行。

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.

推荐答案

先生。埃德回答了自己的问题提供了这个答案,我打扫起来如下:

我敢肯定这是不是这样做的preferred方式,但它的工作原理:

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


  1. 在您的layout.xml定义编辑文本字段,通过改变文字的背景色隐藏文本。还是不行,如果你想看到的文字。

  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.

添加在你的数据库管理器的相应字段。

Add the appropriate field in your database manager.

在您的活动中,插入文本到文本字段。

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

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


当它被保存被保存到数据库的路径。使用 BitmapFactory 来德code这样的图像路径​​:

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

这篇关于保存图像文件路径SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆