samedi 31 janvier 2015

Why can't my image be inserted to the database? It only shows a couple of text in the field of the table

I have a program that will that a picture from the camera and save it to the database but the data shows up when I browse the blob is "[B@22e1ae38" or random letters and numbers and I don't know what is the error.


This is my OnCreate function





@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.addstudentsform);
db=openOrCreateDatabase("ClassManager",MODE_WORLD_READABLE, null);

viewImage = (ImageView)findViewById(R.id.CamPicture);
viewImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "StudPic.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}

});
}



This is my onActivity Results:





@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode,resultCode,data);
if(resultCode==RESULT_OK)
{
if (requestCode == 1)
{

File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles())
{
if (temp.getName().equals("StudPic.png"))
{
f = temp;
break;
}
}

try
{
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
viewImage.setImageBitmap(bitmap);

String path = Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "Default";
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");

try
{

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
StudImage = bos.toByteArray();
f.delete();

}

catch (Exception e)
{
e.printStackTrace();
displayExceptionMessage(e.getMessage());
}

try
{
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, outFile);
outFile.flush();
outFile.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}

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

}
}
}



This is the my CreateScreen where I insert the picture in the database as StudPic which is a blob sql type:





public void CreateScreen (View view)
{

EditText FirstNameText = (EditText)findViewById(R.id.FirstNameText);
EditText StudentIDText = (EditText)findViewById(R.id.StudentIDText);
EditText LastNameText = (EditText)findViewById(R.id.LastNameText);
EditText ContactNumberText = (EditText)findViewById(R.id.ContactNumberText);
EditText EmailAddressText = (EditText)findViewById(R.id.EmailAddressText);

StudentID = Integer.parseInt(StudentIDText.getText().toString());
FirstName = FirstNameText.getText().toString();
LastName = LastNameText.getText().toString();
ContactNumber = Integer.parseInt(ContactNumberText.getText().toString());
EmailAddress = EmailAddressText.getText().toString();

db.execSQL("INSERT INTO MasterStudents (StudPic, StudentID, FirstName, LastName, ContactNumber, EmailAddress) " +
"VALUES ('" + StudImage + "','" + StudentID + "','" + FirstName + "','" + LastName + "','" + ContactNumber + "','" + EmailAddress + "');");

Toast toast = Toast.makeText(getApplicationContext(), "Student Added", Toast.LENGTH_SHORT);
toast.show();
finish();

}



Aucun commentaire:

Enregistrer un commentaire