I'm trying to make an app where the registered user has to upload a profile pic. After that, when the user logs in along with his other credentials the image of the user should also get displayed. I also want to pass the path of the image to the table in MySql. My image is stored ina folder in the server while the user details are stored in the table in the MySql. So when the user Logins along with the credentials the image should also get displayed. But when I'm tryin to register I'm getting an error and the image is not getting uploaded. And my app is getting crashed.
PHP file:
<?php
$con=mysqli_connect("mysql8.000webhost.com","a6391170_root","root123","a6391170_sanjay");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$name1=$_POST["name1"];
$imagepath="pictures1/" . $name1 . ".JPG";
$statement = mysqli_prepare($con, "INSERT INTO `user1` (name, age, username, password,name1,imagepath) VALUES (?, ?, ?, ?,?,?)");
mysqli_stmt_bind_param($statement, "sissss", $name, $age, $username, $password,$name1,$imagepath);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
JAVA FILE:
UploadImage ui=new UploadImage(image,etName1.getText().toString());
ui.execute();
User user = new User(name, age, username, password,name1);
registerUser(user);
break;
case R.id.upImage:
Intent i=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,RESULT_IMAGE);
Toast.makeText(getApplicationContext(),"Selecting Image",Toast.LENGTH_LONG).show();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_IMAGE && resultCode==RESULT_OK && data!=null)
{
Uri selectedImage=data.getData();
imageUp.setImageURI(selectedImage);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
Toast.makeText(getApplicationContext(),"Image Selected",Toast.LENGTH_LONG).show();
}
}
private class UploadImage extends AsyncTask<Void, Void, Void>
{
Bitmap image;
String name1;
public UploadImage(Bitmap image, String name1)
{
this.image=image;
this.name1=name1;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
System.out.println("here");
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
String encodedImage= Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
ArrayList<NameValuePair> dataToSend=new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("image",encodedImage));
dataToSend.add(new BasicNameValuePair("name1",name1));
HttpParams httpRequestParams=getHttpRequestParams();
HttpClient client= new DefaultHttpClient(httpRequestParams);
HttpPost post=new HttpPost(SERVER_ADDRESS + "upload.php");
try
{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
}
}
private HttpParams getHttpRequestParams()
{
HttpParams httpRequestParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,1000*30);
HttpConnectionParams.setSoTimeout(httpRequestParams,1000*30);
return httpRequestParams;
}