在SQLite数据库中存储相机捕获的图像

时间:2013-05-11 20:11:07

标签: android sqlite

我正在使用相机捕捉照片,暂时存储在“ImageView”上,然后通过单击“活动”上的“保存”按钮将其存储在数据库中。但是在数据库中保存图像不起作用。请解决代码中的问题。

   Main File:
     <!-- language: java -->   

   package com.example.expnewbutton;
   import android.net.Uri;
   import android.os.Bundle;
   import android.provider.MediaStore;
   import android.app.Activity;
   import android.content.Intent;
   import android.content.pm.PackageManager;
   import android.graphics.Bitmap;
   import android.view.Menu;
   import android.view.View;
   import android.widget.ImageView;
   import android.widget.Toast;
   public class MainActivity extends Activity
 {
private Uri fileUri;
    Bitmap img;
    databasehelper helper;

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


}

public void cammethod(View w){ 

    try {
        PackageManager packageManager = getPackageManager();
        boolean doesHaveCamera = packageManager
                .hasSystemFeature(PackageManager.FEATURE_CAMERA);

        if (doesHaveCamera) {
            // start the image capture Intent
            Intent intent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            // Get our fileURI
            //fileUri = getOutputMediaFile();

            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, 100);

        }
    } catch (Exception ex) {
        Toast.makeText(getApplicationContext(),
                "There was an error with the camera.",
                Toast.LENGTH_LONG).show();
    }

}


protected void onActivityResult(int requestCode, int resultCode,Intent intent)

{
    if (requestCode == 100) 
     {
        if (resultCode == RESULT_OK) 
        {
            if (intent == null) 
            {
                // The picture was taken but not returned
                Toast.makeText(
                        getApplicationContext(),
                        "The picture was taken and is located here: "
                                + fileUri.toString(), Toast.LENGTH_LONG)
                        .show();
            }
            else 
            {
                // The picture was returned
                Bundle extras = intent.getExtras();
                img=(Bitmap) extras.get("data");    
                ImageView imageView1 = (ImageView)           findViewById(R.id.imageView1);
                imageView1.setImageBitmap((Bitmap) extras.get("data"));
            }
        }
    }
}

public void insertimg(View w)
{ 
    long a=0;
    try{
    helper.insert(img);

    if(a>=1){
        Toast.makeText(getBaseContext(),a+ "Record Successfully Saved",   30).show();
    }

    else{

        Toast.makeText(getBaseContext(),  "Not Saved", 30).show();

    }}
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(), "there is error",5).show();
    }

}



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

   DataBase File:
   <!-- language: java -->

           package com.example.expnewbutton;
           import java.io.ByteArrayOutputStream;
           import android.content.ContentValues;
           import android.content.Context;
           import android.database.Cursor;
          import android.database.SQLException;
          import android.database.sqlite.SQLiteDatabase;
          import android.database.sqlite.SQLiteOpenHelper;
          import android.graphics.Bitmap;
         import android.graphics.Bitmap.CompressFormat;
         import android.util.Log;


        public class databasehelper extends SQLiteOpenHelper{

    final static String databasename="Imagedb";
    final static int databaseversion=1;

        public databasehelper(Context ctx){
      super(ctx,databasename,null,databaseversion);

    }

     @Override
     public void onCreate(SQLiteDatabase db) {
    try{

         Log.d("tag4545","database");   
      db.execSQL("create table mypic(pic BLOB)");

    }
    catch(SQLException e){e.printStackTrace();
    }

    }

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

    db.execSQL("drop table if Exists mypic");

    onCreate(db);

       }

        public long insert(Bitmap img ) {

    SQLiteDatabase base=getWritableDatabase();
    byte[] data = getBitmapAsByteArray(img); // this is a function
       ContentValues value=new ContentValues();
      value.put("pic",data);

       long a= base.insert("mypic", null, value);  

      return a; 
       }

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


    XML file is
  <!-- language: xml -->

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:context=".MainActivity"
     android:background="@drawable/gb2"
 >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="Cam"

    android:onClick="cammethod" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_marginLeft="46dp"
    android:layout_toRightOf="@+id/button1"
    android:onClick="insertimg"
    android:text="Save" />

  </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

无法在代码中捕获更多问题,并且没有任何可用日志。

我只喜欢两个人​​。 :)

  1. 您忘记初始化数据库类..

    helper.insert(img);
    

    所以在使用上面的代码行之前,只需在Activity的onCreate()中初始化数据库类。像,

    databasehelper helper = new databasehelper(this);
    
  2. a的值将在何处更改?

    所以它应该是,

    long a=0;
    try{
    a = helper.insert(img);
    
  3. 无论如何,这两个可以帮助你继续前进。