So i am using Volley for my app to send and receive data and store them in sqlite i have done a lot of googling and i learned that the sqlite datatype should be blob and i am using image data filed in sql server i know many said it is bad practice and i should store file path of the image and retrieve it in android well feel free to help me to do any of these two ways thanks. now in my sql server table i have few columns and one of them should be image i receive all the parameters to android now i suppose myImage are base64 encoded feel free to correct me now i display every column on the screen but i want to know how i can display this image i get here is part of the code how i display the text
Fetching user from Sqlite: {Student_ID=222002, Parent_Name=muha, Parent_ID=1110044, Student_Name=Sameer, MyImage=MS5qcGc=, Username=muhaa}
this is how i fetch and display from sqlite
// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();
String Parent_Name = user.get("Parent_Name");
String Username = user.get("Username");
String Parent_ID = user.get("Parent_ID");
String MyImage = user.get("MyImage");
String Student_ID = user.get("Student_ID");
String Student_Name = user.get("Student_Name");
txtParent_Name.setText(Parent_Name);
txtUsername.setText(Student_ID);
txtParent_ID.setText(Student_Name);
/* as i searched and found out this is what should i do to display the image but it doesnt work
SkImageDecoder::Factory returned null
*/
byte[] imageAsBytes = Base64.decode(MyImage.getBytes(), Base64.DEFAULT);
pic.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
now here is my php file where i insert to sql server
<?php
ini_set('display_errors', 1);
error_reporting(~0);
if (isset($_POST['Parent_ID']) && isset($_POST['Parent_Name']) && isset($_POST['Username'])&& isset($_POST['password'])&& isset($_FILES['photo']['name'])) {
$Parent_ID = $_POST['Parent_ID'];
$Parent_Name = $_POST['Parent_Name'];
$Username = $_POST['Username'];
$password = $_POST['password'];
// This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$pic=($_FILES['photo']['name']);
$base64 =base64_encode($pic);
$serverName = "HP-PC";
$userName = "sa";
$userPassword = 'P@ssw0rd';
$dbName = "Attendance";
$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO Parents(Parent_ID, Parent_Name, Username, Password, MyImage) VALUES(?, ?, ?, ?, '$base64')";
$params = array($_POST['Parent_ID'], $_POST['Parent_Name'], $_POST['Username'], $_POST['password'],$base64);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}else{
echo "Record add successfully";
}
sqlsrv_close($conn);}
// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
// Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
// Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
Aucun commentaire:
Enregistrer un commentaire