保存图像文件路径到sqlite数据库
问题描述:
我还没有找到一个实际的例子,具体涉及到保存图像的文件路径,你刚刚拿着相机应用程序到应用程序中的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());
的条目在仿真器中起作用。但奇怪的是,在手机上无法使用。埃德
答
先生回答了自己的问题提供了这个答案,我清理了如下:
我敢肯定这是不是这样做的首选方式,但它的工作原理:
在您的layout.xml中定义一个编辑文本字段,并通过将文本的颜色更改为背景来隐藏文本。或者,如果你想看到文本。
在您的数据库管理器中添加相应的字段。
-
在您的活动中,将文本插入文本字段。
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);
如果你的问题解决了,你应该张贴的溶液,作为一个答案(是的,这是完全可以接受的回答你自己的问题)。如果您想等待更好的答案,只是不接受它。 (您会注意到这里已经解决了数千个问题,但是标题中只有很少的问题已经解决了。) –