We are trying to upload image using the following code snippet:
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
Uri selectedImage = intent.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 filePath = cursor.getString(columnIndex);
cursor.close();
if(bmp != null && !bmp.isRecycled())
{
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
iv.setBackgroundResource(0);
iv.setImageBitmap(bmp);
File file = new File(filePath);
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root", "virus");
PreparedStatement ps =
con.prepareStatement("Update user_info set image=? WHERE ID=\""+id+"\"");
ps.setBinaryStream(1,fis,(int)file.length());
ps.executeUpdate();
ps.close();
fis.close();
}
catch(Exception e)
{
Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
}
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
After uploading in mysql DB we are getting [BLOB - 60.1KiB].. Whether image uploaded successfully? we are trying to retrieve it through the following code
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root","virus");
Statement st = con.createStatement();
String query = "Select image from user_info where ID=\""+id+"\"";
PreparedStatement pstmt = con.prepareStatement(query);
ResultSet rset = pstmt.executeQuery();
rset.next();
byte[] blob = rset.getBytes(1);//getBlob(1);
if(blob != null) {
byte[] decodedString = Base64.encode(blob, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
iv.setImageBitmap(decodedByte);
} else {
iv.setImageResource(R.drawable.chit_chat); }
But we are unable to display in the image view.