0

我的应用程序应该使用相机拍照,然后将它们保存到 SQLite 数据库。这是通过打开相机拍摄照片来完成的,然后图像返回到图像视图,然后用户单击保存以将图像保存到数据库中。

问题

在图像返回到图像视图并且用户单击保存按钮后,应用程序冻结,唯一显示的是“不幸的是,Dress(应用程序的名称)已停止响应”,其中没有错误。

我试过什么

我尝试通过注释我们的函数来进行一些调试,并且我已将其范围缩小到 db.open() 函数,因此似乎一旦它尝试与数据库建立任何连接,它就会冻结。我以前从未使用过 android 或 android 数据库(专家级别的菜鸟),我已经做得足够了,但仍然无法解决问题。

这就是我一直使用的教程

http://www.youtube.com/watch?v=Xq8m82zgt1s

//This file triggers the camera returns the images and makes the call to the database
package dress.advance;

import java.io.FileNotFoundException;

import org.apache.http.util.EntityUtils;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Upload extends Activity {
    protected static final int SELECT_IMAGE = 0;
    protected static final int CAMERA_ACTIVITY = 0;
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    protected static final String Upload = null;
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 0;
    private int count = 0;
    private Uri fileUri;
    DBAdapter link;
    ImageView CaputredImage;
    ImageView browsedImage;
    Bitmap theCapturedImage;
     private static int RESULT_LOAD_IMAGE = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        browsedImage=(ImageView) findViewById(R.id.ImageBrowse);

        Button browseButton = (Button) findViewById(R.id.browse);

        browseButton.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                //startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, 1);

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



        }       
    });

        Button save = (Button) findViewById(R.id.ImageSaveButton);
        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                if(!(CaputredImage == null)){

                    Img testim = new Img(theCapturedImage, "img"+getCount());           
                    link.open();
                    /*link.createImgEntry(testim);
                    link.close();
                    */
                }
            }

        });


        CaputredImage=(ImageView) findViewById(R.id.ImageCapture);


        //Declaration of button to link to camera trigger
        Button CaptureButton = (Button) findViewById(R.id.capture);
        //Listener for button
        CaptureButton.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                Intent startCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(startCamera, 0);

                Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
                mIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
                startActivityForResult(mIntent, CAMERA_ACTIVITY);

            }       
        });

    }






protected void onActivityResult(int requestCode, int resultCode, Intent data)
{


    if(requestCode == 0){
        super.onActivityResult(requestCode, resultCode, data);
        //setCount(getCount() + 1);
        theCapturedImage = (Bitmap) data.getExtras().get("data");
        CaputredImage.setImageBitmap(theCapturedImage);

        fileUri = getOutputMediaFileUri(Upload); // create a file to save the image
        data.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
        Img testim = new Img(theCapturedImage, "img"+getCount());

        link.open();
        link.createImgEntry(testim);
        link.close();

    }
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        browsedImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        ContentResolver cr = getContentResolver();
        try {
            Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(cr, "", "name", "now"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }}

    private Uri getOutputMediaFileUri(String upload2) {
    // TODO Auto-generated method stub
    return null;
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

}    





    // this is my db helpper
    package dress.advance;

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.BitmapFactory;

import android.util.Log;



public class DBAdapter {

    public final static String Key_ROWID = "_id";
    public final static String Key_NAME = "name";
    public final static String TAG = "socialNatwork";
    public final static String DATABASE_NAME = "SocialNetwork";
    public final static String Key_IMG = "image";

    public final static String DATABASE_TABLE = "PESITIMAGE";

    public final static int DATABASE_VERSION = 1;

    //i think the problem might be here 
    public final static String DATABASE_CREATES =
            "create table PESITIMAGE(_id integer primary key autoincrement, image BLOB not null, name text not null);  ";



    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    private Context context;

    public DBAdapter (Context ctx){
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);

    }

    private static class DatabaseHelper extends SQLiteOpenHelper{

        DatabaseHelper(Context context){

            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            Log.i(TAG, "Creating the database");
            db.execSQL(DATABASE_CREATES);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.w(TAG, "upgrading database from version" + oldVersion 
                    + " to" 
                    + newVersion +", which will destroy all data");
            db.execSQL("DROP TABLE IF EXIST PESITIMAGE");
            onCreate(db);

        }
    }

        public DBAdapter open() throws SQLException{
            Log.i(TAG, "opening the database");
            db = DBHelper.getWritableDatabase();
            return this;

        }

        public void close(){
            Log.i(TAG, "closing the database");
            db = DBHelper.getWritableDatabase();
            DBHelper.close();

        }

        public void createImgEntry(Img img){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            img.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
            ContentValues cv = new ContentValues();
            cv.put(Key_IMG, out.toByteArray());
            cv.put(Key_NAME, img.getName());
            long j = db.insert(DATABASE_TABLE, null, cv);
            Log.i("Sakshi", String.valueOf(j));
        }

        public Img getFirstImgFormats() throws SQLException{
            Cursor cur = db.query(true, 
                    DATABASE_TABLE,
                    new String[]{Key_IMG, Key_NAME},
                    null, null, null, null, null, null);
            if(cur.moveToFirst()){
                byte[] blob = cur.getBlob(cur.getColumnIndex(Key_IMG));
                Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
                String name = cur.getString(cur.getColumnIndex(Key_NAME));

                cur.close();
                return new Img(bmp,name);
            }
            cur.close();
            return null;

        }

        public Img getImgFromDB(String n) throws SQLException{
            Cursor cur = db.query(true,
                    DATABASE_TABLE,
                    new String[]{Key_IMG, Key_NAME},
                    Key_NAME+"=\""+n+"\"", null, null, null, null, null);
            if(cur.moveToFirst()){
                byte[] blob = cur.getBlob(cur.getColumnIndex(Key_IMG));
                Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
                String name = cur.getString(cur.getColumnIndex(Key_NAME));
                cur.close();
                return new Img(bmp,name);
            }
            cur.close();
            return null; 

        }

        public Img getImg() throws SQLException{
            Cursor cur = db.query(true, 
                    DATABASE_TABLE,
                    new String[]{Key_IMG, Key_NAME},
                    null, null, null, null, null, null);
            if(cur.moveToLast()){
                byte[] blob = cur.getBlob(cur.getColumnIndex(Key_IMG));
                Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
                String name = cur.getString(cur.getColumnIndex(Key_NAME));

                cur.close();
                return new Img(bmp,name);
            }
            cur.close();
            return null;

        }

    }
4

0 回答 0