如何从另一个类

时间:2015-10-27 16:56:59

标签: java android database sqlite

我需要从我的其他活动访问在一个类中创建的SQLite数据库。 我想从public void getImage(View view) {访问,但是当我使用它时应用程序崩溃了。 这是我的活动:

public class CameraToDatabase extends Activity {
    private static final int CAMERA_REQUEST = 1888;
    public ImageView imageView1;
    Imagehelper help = new Imagehelper(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_to_database);


        Button B = (Button) this.findViewById(R.id.camera);
        B.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            help.insert(byteArray);
        }
    }

    public void getImage(View view) {

        Cursor c = help.getAll();
        if (c.moveToNext())
        {
            byte[] byteArray = c.getBlob(0);
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            imageView1.setImageBitmap(bmp);

        }
        c.close();

    }

这是我创建数据库的地方:

public class Imagehelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "clothes.db";
    private static final int SCHEMA_VERSION = 3;

    public Imagehelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insert(byte[] bytes) {
        ContentValues cv = new ContentValues();

        cv.put("imageblob", bytes);
        Log.e("inserted", "inserted");
        getWritableDatabase().insert("Image", "imageblob", cv);

    }

    public Cursor getAll() {
        return (getReadableDatabase().rawQuery("SELECT column1 FROM Image", null));
    }



}

}

3 个答案:

答案 0 :(得分:0)

你的桌子'图片'没有列' column1'。它有' _id'和on' imageblob',你可以在onCreate方法中看到。所以' getAll'方法永远不会工作,因为它执行SELECT#FROM FROM Image'!

查看正在讨论的崩溃的错误消息/堆栈跟踪会很有帮助。

答案 1 :(得分:0)

放置

new Imagehelper(this)

在oncreate之后。

你的创作应该是这样的:

private static final int CAMERA_REQUEST = 1888;
    public ImageView imageView1;
    Imagehelper help ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_to_database);
        help = new Imagehelper(this);

        Button B = (Button) this.findViewById(R.id.camera);
        B.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

答案 2 :(得分:0)

现在你的主动活动应该像:

package com.solution.sa.stack_camerasql;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_REQUEST = 1888;
    public ImageView imageView1;
    Imagehelper help ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        help = new Imagehelper(this);
        imageView1= (ImageView) this.findViewById(R.id.myimg);
        Button B = (Button) this.findViewById(R.id.camera);
        B.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            help.insert(byteArray);
            getImage();
        }
    }

    public void getImage() {

        Cursor c = help.getAll();
        if (c.moveToNext())
        {
            byte[] byteArray = c.getBlob(0);
            Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            imageView1.setImageBitmap(bmp);


        }
        c.close();

    }
}

和你的Imgaehelper Class一样:

package com.solution.sa.stack_camerasql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by asim on 10/27/2015.
 */
public class Imagehelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "clothes.db";
    private static final int SCHEMA_VERSION = 3;

    public Imagehelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insert(byte[] bytes) {
        ContentValues cv = new ContentValues();

        cv.put("imageblob", bytes);
        Log.e("inserted", "inserted");
        getWritableDatabase().insert("Image", "imageblob", cv);

    }

    public Cursor getAll() {
        return (getReadableDatabase().rawQuery("SELECT  imageblob FROM Image", null));
    }



}

现在你只能设法只显示最后拍摄的图像而不是所有图像上的循环