我正在研究如何使用volley将图像从Android应用程序上传到服务器(这里我使用wamp)。对我来说,图像不会保存在mysql数据库中,而是成功存储在我制作的文件夹中?请帮我纠正这个问题?我将在下面发布我的代码:
我在android中使用的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView textView;
Button select,upload;
private final static int PICK_IMAGE_REQUEST=1;
ImageView imageView;
String FileName,FilePath;
private Bitmap bitmap;
public static final String FILE_UPLOAD_URL="http://192.168.187.1/progressdemo/upload.php";
private String KEY_IMAGE = "image";
private String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
select=(Button)findViewById(R.id.button);
upload=(Button)findViewById(R.id.button2);
imageView=(ImageView)findViewById(R.id.imageView);
select.setOnClickListener(this);
upload.setOnClickListener(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filepath=data.getData();
FilePath=filepath.toString();
FileName=FilePath.substring(FilePath.lastIndexOf("/")+1);
textView.setText(FileName);
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
if(v==select)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select file"),PICK_IMAGE_REQUEST);
}
else if (v==upload)
{
uploadfile();
}
}
private void uploadfile()
{
final ProgressDialog progress=ProgressDialog.show(this,"Uploading image...","....Please wait..",false,false);
StringRequest stringRequest=new StringRequest(Request.Method.POST, FILE_UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
progress.dismiss();
//Showing toast message of the response
Toast.makeText(MainActivity.this, s , Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
progress.dismiss();
//Showing toast message of the response
Toast.makeText(MainActivity.this, volleyError.toString() , Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams() throws AuthFailureError{
String name=textView.getText().toString();
String image=getStringImage(bitmap);
Map<String,String> params=new Hashtable<String, String>();
params.put(KEY_IMAGE,image);
params.put(KEY_NAME,name);
return params;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public String getStringImage(Bitmap bmp)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] imagebytes=baos.toByteArray();
String encodedimage= Base64.encodeToString(imagebytes,Base64.DEFAULT);
return encodedimage;
}
}
我在php中使用的代码:
<?php
header('Content-type:bitmap;charset=utf-8');
if(isset($_POST["image"]))
{
$encoded_string=$_POST["image"];
$name=$_POST["name"];
$decoded_string=base64_decode($encoded_string);
$path='images/'.$name;
$file=fopen($path,'w');
$is_written=fwrite($file,$decoded_string);
fclose($file);
if($is_written>0)
{
$con=mysqli_connect('localhost','root','','progressdemo');
echo "connection success\n";
$path = mysqli_real_escape_string($con, $path);
$query="insert into volleyupload(photo,name)values('".$path."','".$name."');";
$result=mysqli_query($con,$query);
if($result)
{
echo "success";
}
else{
echo "failure";
}
mysqli_close($con);
}
}
?>