我只是对用户可以从文件存储器中选择照片并将其设置为个人资料照片的方式感到困惑,在后台将照片发送到服务器,直到现在我已经能够提供用户选择选项,然后在个人资料图像中设置该照片,但是我仍然不知道我该发送什么到服务器,我不知道我需要什么API,正确的格式是什么。 我当前的代码是:
playerImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryintent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryintent.setType("image/*");
galleryintent.putExtra("aspectX", 1);
galleryintent.putExtra("aspectY", 1);
galleryintent.putExtra("scale", true);
galleryintent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(galleryintent, i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == i && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
assert selectedImage != null;
Log.i("selected Image",selectedImage.toString());
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedImage);
String image = getStringImage(bitmap);
Log.d("image",image);
//passing the image to volley
SendImage(image);
} catch (IOException e) {
e.printStackTrace();
}
saveImage(bitmap);
playerImage.setImageURI(selectedImage);
// playerImage.setImageBitmap();
pathToImage = selectedImage.getPath();
//stuff to do on click button upload cover??
}
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
public static void saveImage(Bitmap bitmap) {
OutputStream output;
String recentImageInCache;
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath()
+ "/YOUR_APP/profile");
dir.mkdirs();
// Create a name for the saved image
File file = new File(dir, username+".jpg");
try {
output = new FileOutputStream(file);
// Compress into png format image from 0% - 100%
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我不知道的地方
private void SendImage( final String image) {
final ProgressDialog loading = ProgressDialog.show(Matchboard.this, "updating in...", null, true, true);
RequestQueue requestQueue= Volley.newRequestQueue(this);
String url="http://www.judgement6.com/judgement_files/profileImage.php";
final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("uploade",response);
try {
loading.dismiss();
JSONObject jsonObject = new JSONObject(response);
Log.e("returns", String.valueOf(jsonObject));
if (jsonObject.toString().equals("image uploaded successfully")){
Toast.makeText(Matchboard.this,"image uploaded successfully",Toast.LENGTH_LONG).show();
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Matchboard.this, "No internet connection", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new Hashtable<String, String>();
params.put("image", image);
params.put("email",userEmail);
return params;
}
};
{
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
}
我还希望每次用户登录时都从服务器下载该照片,这样他/她就不必每次都进行设置。 这是我的profileImage.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
require_once('connect.php');
$image=$_POST['image'];
$email=$_POST['email'];
$sql = "update users2 set profileImage='$image' where email='$email'";
if(mysqli_query($con,$sql)){
echo 'image uploaded successfully';
}
else
{
echo 'oops! Please try again!';
}
}
?>
我想要做的就是以url形式获取图像,例如xyz.com/image/1.png类型的东西