尝试保存位图时在数据库中获取空值

时间:2016-03-04 08:03:40

标签: android sqlite bitmap save

我正在尝试将相机拍摄的照片保存到我的SQLite数据库中,但我在数据库中获取的所有内容都为空。我的转换错了吗?我看不出我做错了什么。

这是我的片段:

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;



public class SpendingDetails extends Fragment {
private static final String TAG = "Spending Fragment";
String id;
View view;
String cursorNote;
String cursorTime;
String cursorDate;
String cursorLatitude;
String cursorLongitude;
String cursorAmount;
String cursorPicture;
ImageView image;
int REQUEST_CODE = 1;
int RESULT_OK =1;
MyDBHandler db;
SQLiteDatabase dataBase;
Cursor cursor;
Bitmap BMP;
public SpendingDetails() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    id = getArguments().getString("id");

    view= inflater.inflate(R.layout.more_details, container, false);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
     db = new MyDBHandler(getActivity(),null,null,3);
     dataBase = db.getWritableDatabase();
    cursor=dataBase.rawQuery("SELECT * FROM spendings_table WHERE _id=" + id, null);

   // Toast.makeText(getContext(), "ID passed : " + id, Toast.LENGTH_SHORT).show();
    TextView note = (TextView) view.findViewById(R.id.note);
    TextView time = (TextView) view.findViewById(R.id.time_details);
    TextView date = (TextView) view.findViewById(R.id.date);
     image = (ImageView) view.findViewById(R.id.imageView);







    if( cursor != null && cursor.moveToFirst() ){
         cursorNote = cursor.getString(cursor.getColumnIndexOrThrow("_note"));
        cursorTime = cursor.getString(cursor.getColumnIndexOrThrow("_time"));
        cursorDate = cursor.getString(cursor.getColumnIndexOrThrow("_date"));




    }

    note.setText(String.valueOf(cursorNote));
    time.setText(String.valueOf(cursorTime));
    date.setText(String.valueOf(cursorDate));
    byte[] picture = cursor.getBlob(cursor.getColumnIndexOrThrow("_picture"));
    image.setImageBitmap(BitmapFactory.decodeByteArray(picture,0,picture.length));


    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(i, REQUEST_CODE);


        }// end onClick
    });



}
 public void onActivityResult(int requestcode, int resultcode, Intent data){


     if (requestcode==REQUEST_CODE){

          Bundle bundle = data.getExtras();
         BMP =(Bitmap) bundle.get("data");
         image.setImageBitmap(BMP);

         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         BMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
         db.addPicture(stream.toByteArray());


     }


}




}

我的DBHandler:

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQuery;
import android.widget.Toast;


public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "payzati_spendings.db";
    private static final String TABLE_NAME = "spendings_table";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_TRANSACTION = "_transactionType";
    private static final String COLUMN_LOCATION = "_location";
    private static final String COLUMN_DATE = "_date";
    private static final String COLUMN_TIME = "_time";
    private static final String COLUMN_LONGITUDE = "_longitude";
    private static final String COLUMN_LATITUDE = "_latitude";
    private static final String COLUMN_NOTE = "_note";
    private static final String COLUMN_PICTURE = "_picture";
    private static final String COLUMN_AMOUNT = "_amount";


    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, null, version);
    }



    @Override
    public void onCreate(SQLiteDatabase db) {

        String query = " CREATE TABLE "+ TABLE_NAME + "( " +
                COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                COLUMN_TRANSACTION+" TEXT, "+
                COLUMN_AMOUNT+" DOUBLE, "+
                COLUMN_LOCATION+" TEXT, "+
                COLUMN_TIME+" TEXT, "+
                COLUMN_DATE+" TEXT, "+
                COLUMN_NOTE+" TEXT, "+
                COLUMN_LONGITUDE+" DOUBLE, "+
                COLUMN_LATITUDE+" DOUBLE, "+
                COLUMN_PICTURE+" BLOB "+
                ")";
        db.execSQL(query);

    }

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

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }

    // public void addRow(SpendingRow spendingRow){
    public void addRow(String transaction, double amount,String note, String location, String date,String time, double longitude, double latitude){

        ContentValues values = new ContentValues();
        values.put(COLUMN_TRANSACTION, transaction);
        values.put(COLUMN_AMOUNT, amount);
        values.put(COLUMN_LOCATION, location);
        values.put(COLUMN_DATE, date);
        values.put(COLUMN_TIME, time);
        values.put(COLUMN_LONGITUDE, longitude);
        values.put(COLUMN_LATITUDE, latitude);
        values.put(COLUMN_NOTE, note);


        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);


    }
    public void addPicture(byte[] bytes){

        ContentValues values = new ContentValues();
        values.put(COLUMN_PICTURE, bytes);

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);

    }



}

0 个答案:

没有答案