我正在尝试创建一个注册用户必须上传个人资料照片的应用。 之后,当用户与其他凭证一起登录时,还应显示用户的图像。我还想将图像的路径传递给MySql中的表。我的图像存储在服务器的一个文件夹中,而用户详细信息存储在MySql的表中。因此,当用户登录凭证时,还应显示图像。但是,当我试着注册时,我收到了一个错误,图像没有上传。我的应用程序正在崩溃。
PHP文件:
<?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);
&GT;
JAVA文件:
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;
}
答案 0 :(得分:0)
这可能对您有所帮助
<?
$con=mysqli_connect("mysql8.000webhost.com","a6391170_root","root123","a6391170_sanjay");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$UploadedFileName=$_FILES['name1']['name'];
if($UploadedFileName!='')
{
$upload_directory = "pictures1/";
$imagepath=$upload_directory.$UploadedFileName;
if(move_uploaded_file($_FILES['name1']['tmp_name'], $upload_directory.$UploadedFileName))
{
$statement = mysqli_prepare($con, "INSERT INTO `user1`(name, age, username, password,name1,imagepath)
VALUES(?, ?, ?, ?, ?, ?) ");
mysqli_stmt_bind_param($statement, "sissss", $name, $age, $username, $password,$UploadedFileName,$imagepath);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
}
}
mysqli_close($con);
?>