我是Android的新手。我刚刚在Android 2.1中创建了一个带有256MB Android-SD卡的AVD。我在里面插入了两张图片。我已经使用DDMS透视图做到了这一点。并且图像现在被存储在SDcard的DCIM文件夹中的文件夹100ANDRO中。现在我想创建一个应用程序,允许用户通过浏览文件夹来选择图像,并且需要将相应的图像保存到数据库android-sqlite中。
有没有人能帮我找个合适的方法呢?提前谢谢。
发布于 2011-05-18 19:36:55
我找到了一种方法来解决这个问题。
我已经为UPLOAD和单击操作创建了一个按钮,如下所示。
upload.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
});我已经覆盖了这个方法和下面的同一个类。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
{
if (resultCode == RESULT_OK)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
imgView.setImageBitmap(bitmap);
int size = bitmap.getWidth() * bitmap.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();}
String bb = out.toString();
byte[] x = out.toByteArray();
image_value.setTag(x);
image_value.setText(filePath);
}catch(Exception e)
{}
}
}
}在这里,image_value表示xml文件中的隐藏文本视图。我已经传递了图像位置和字节的值作为文本视图的值和标记。稍后,我将这些字节保存到数据库中,以便稍后显示。它工作得很好。
感谢所有人。
https://stackoverflow.com/questions/6026081
复制相似问题