android日记应用程序 - 图像进入sqlite数据库

时间:2014-04-21 16:38:39

标签: java android database sqlite

我正在写日记安卓应用,但该应用已停止

我成功将文本放入数据库(sqlite) 我想把手机画廊的照片也放到数据库中 添加照片代码后,应用程序已停止 我不知道如何解决这个问题

请帮我看看代码有什么问题

public class EditActivity extends Activity {
private EditText editText;
private Button phoBtn;
private Button saveBtn;
private Button cancalBtn;
private DBHelper dbHelper;
private int id;

SQLiteDatabase db;
private  String selectedImagePath;
private static final int SELECT_PICTURE = 1;

ImageView iv;
Uri selectedImageUri;
String ivimage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);
    editText = (EditText) findViewById(R.id.editText);
    phoBtn = (Button) findViewById(R.id.photos);
    saveBtn = (Button) findViewById(R.id.save);
    cancalBtn = (Button) findViewById(R.id.cancel);
    id = getIntent().getIntExtra("_id", -1);


    iv=(ImageView)findViewById(R.id.imageView1);

    System.out.println(id);
    dbHelper = new DBHelper(this);

    if (id != -1) {
        Cursor cursor = dbHelper.query(
                "select name,content,dt from DIARY where _id=?",
                new String[] { String.valueOf(id) });
        cursor.moveToFirst();
        String content = cursor.getString(1);
        editText.setText(content);
        String i=cursor.getString(cursor.getColumnIndex("image"));
         Uri imgUri=Uri.parse(i);
         iv.setImageURI(imgUri); 

        saveBtn.setOnClickListener(updateClickListener);
    }else
    {
        db.execSQL("insert into DIARY values('"+selectedImageUri+"')");

        saveBtn.setOnClickListener(saveClickListener);
    }

    cancalBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(EditActivity.this,MainActivity.class);
            startActivity(intent);
        }
    });

    phoBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);
        }
    });
}

View.OnClickListener updateClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        String text = editText.getText().toString();
        ContentValues values = new ContentValues();
        values.put("content", text);

        String ivimage=iv.getContext().toString(); 
         values.put("image", ivimage);


        dbHelper.update(values,String.valueOf(id));
        Intent intent = new Intent(EditActivity.this,MainActivity.class);
        startActivity(intent);
    }
};

EditText et =null;
String text;
View.OnClickListener saveClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        et = new EditText(EditActivity.this);
        text = editText.getText().toString();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(new Date());
        et.setText(date);
        new AlertDialog.Builder(EditActivity.this)  
        .setTitle("My Diary")  
        .setIcon(android.R.drawable.ic_dialog_info)  
        .setView(et)  
        .setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = et.getText().toString();
                ContentValues values = new  ContentValues() ;
                values.put("name", name);
                values.put("content", text);
                values.put("image", ivimage);
                dbHelper.insert(values);
                Intent intent = new Intent(EditActivity.this,MainActivity.class);
                startActivity(intent);
            }
        })  
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        })
        .show();
    }
};

 public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedImageUri = data.getData();

                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                iv.setVisibility(View.VISIBLE);
                iv.setImageURI(selectedImageUri);
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

  

java.lang.IllegalStateException:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保Cursor已正确初始化。

此例外来自:

Cursor cursor = dbHelper.query(
        "select name,content,dt from DIARY where _id=?",
        new String[] { String.valueOf(id) });
cursor.moveToFirst();
String content = cursor.getString(1);
editText.setText(content);
String i=cursor.getString(cursor.getColumnIndex("image"));

您的光标不包含image列,getColumnIndex()返回-1

要解决此问题,请将image添加到投影中:

select name,content,dt,image from ...

此外,您应该检查moveToFirst()的返回值,以确保在尝试读取列值之前光标指向有效行。