0

我是安卓新手。我创建了一个应用程序,用于使用 android 手机拍照并将该照片保存在 Sqlite 数据库中。但是这段代码没有运行。我无法在程序中找到错误。请帮我。

public class MainActivity extends Activity {

    private static final int CAMERA_REQUEST = 0; 
    public ImageView imageView; 

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

        imageView =(ImageView) findViewById(R.id.image);
         Button B = (Button) this.findViewById(R.id.camera);

         B.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }

         });

    }


     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
          ImageHelper help= new ImageHelper(this);
            if (requestCode == CAMERA_REQUEST) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data");   
                imageView.setImageBitmap(photo);  
                ByteArrayOutputStream out=new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.PNG,100,out);
                byte ByteArr[]=out.toByteArray();
                help.insert(ByteArr);
            }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

} 

数据库代码

public class ImageHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME="image";
private static final int DATABASE_VERSION=1;

public ImageHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@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) {
    // TODO Auto-generated method stub

}

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

public void insert(byte Byte[]){
     ContentValues cv=new ContentValues();
     cv.put("imageblob",Byte);
    Log.e("inserted","inserted");
    getWritableDatabase().insert("image","imageblob",cv);

}


public byte[] getImage(Cursor c)
{
   return(c.getBlob(1));
}
}
4

1 回答 1

1

用 LogCat 说起来会更容易......错误是什么?

这不是问题,但我建议您将图像 URI 存储为字符串而不是 sqlite 数据库中的字节数组。

从字节数组中获取 URI:

        String path = Images.Media.insertImage(getContentResolver(), ImageInByte,
                "title", null);

        Uri imageUri = Uri.parse(path);

        String uriString = uriMyLogo.toString() ;

并从 URI 获取图像

                Uri imageUri = Uri.parse(uriString);

                Bitmap image = null;
                try {
                    image = Media.getBitmap(this.getContentResolver(), imageUri );
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
于 2013-07-22T13:02:42.460 回答