在Sqlite中存储从库中选择的图像路径

时间:2014-03-31 23:01:55

标签: android eclipse sqlite

我试图在sqlite数据库中保存从图库中选择的图像的路径。我可以选择图像,但是当我关闭窗口以创建新数据库时会发生异常。

代码如下;

package sekth.droid.sqlite.Activities;


public class NuevaNotaActivity extends Activity {

public static int resultCode = 10;
private static int RESULT_LOAD_IMAGE = 1;


private Button btnAgregar;
private EditText txtNota;
private EditText txtDeno;
private EditText txtVariedad;
private EditText txtAno;
private ImageButton btnImage; 
private NotasDataSource dataSource;

private String picturePath;
public byte[] imageWine;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nueva_nota);

    dataSource = new NotasDataSource(this);
    dataSource.open();

    txtNota = (EditText) findViewById(R.id.txtNota);
    txtDeno = (EditText) findViewById(R.id.txtDeno);
    txtVariedad = (EditText) findViewById(R.id.txtVariedad);
    txtAno = (EditText) findViewById(R.id.txtAno);
    btnAgregar = (Button) findViewById(R.id.btnAgregar);
    btnAgregar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String textoNota = txtNota.getText().toString();
            String textoDeno = txtDeno.getText().toString();
            String textoVariedad = txtVariedad.getText().toString();
            String textoAno = txtAno.getText().toString();


            if (textoNota.length() != 0) {
                dataSource.crearNota(textoNota,textoDeno,textoVariedad,textoAno,picturePath);
                setResult(RESULT_OK);
                finish();
            } else {
                Toast.makeText(getApplicationContext(),
                        "No ha introducido texto", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });

    btnImage = (ImageButton) findViewById(R.id.imageSelector);
    btnImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

           Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    dataSource.close();
    super.onPause();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        picturePath = cursor.getString(column_index);
        Toast.makeText(getApplicationContext(),picturePath, Toast.LENGTH_SHORT).show();

    cursor.close(); 
    }

}


public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

}

例外是:

03-31 18:28:39.339: E/AndroidRuntime(2298): FATAL EXCEPTION: main
03-31 18:28:39.339: E/AndroidRuntime(2298): Process: sekth.droid.sqlite, PID: 2298
03-31 18:28:39.339: E/AndroidRuntime(2298): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/sekth.droid.sqlite/databases/Notas
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1437)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at sekth.droid.sqlite.ddbb.NotasDataSource.crearNota(NotasDataSource.java:41)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at sekth.droid.sqlite.Activities.NuevaNotaActivity$1.onClick(NuevaNotaActivity.java:67)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.view.View.performClick(View.java:4424)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.view.View$PerformClick.run(View.java:18383)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.os.Handler.handleCallback(Handler.java:733)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.os.Looper.loop(Looper.java:137)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at java.lang.reflect.Method.invoke(Method.java:515)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-31 18:28:39.339: E/AndroidRuntime(2298):     at dalvik.system.NativeStart.main(Native Method)

有人可以帮助我吗?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

当你做这个时,他们是一个抓住

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    dataSource.close();
    super.onPause();
}

并且您正在等待ActvityResult已经关闭数据库,您需要重新打开它才能使用它。 将上面的函数更改为this并重试。

@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        dataSource.close();
        super.onDestroy();
    }