【发布时间】:2016-06-16 11:18:39
【问题描述】:
在我的应用程序中,我试图通过 LAN 连接将照片上传到我的服务器。我从本教程开始:Sending Image to Server,但发现它使用了已弃用的方法——具体来说是 HttpParams 和 HttpClient。搜索后,我找到了 HttpUrlConnection,并正在尝试修复应用程序以上传照片。
这是我的 Android 代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int RESULT_LOAD_IMAGE = 1;
private static final String SERVER_ADDRESS = "http://my__LAN_IP_address/SavePicture.php";
HttpURLConnection urlConnection = null;
ImageView imageToUpload, downloadedImage;
Button bUploadImage, bDownloadImage;
EditText uploadImageName, downLoadImageName;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
downloadedImage = (ImageView) findViewById(R.id.downloadedImage);
bUploadImage = (Button) findViewById(R.id.bUploadImage);
bDownloadImage = (Button) findViewById(R.id.bDownloadImage);
uploadImageName = (EditText) findViewById(R.id.etUploadImage);
downLoadImageName = (EditText) findViewById(R.id.etDownloadName);
imageToUpload.setOnClickListener(this);
bUploadImage.setOnClickListener(this);
bDownloadImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.imageToUpload:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
case R.id.bUploadImage:
Bitmap image = ((BitmapDrawable) imageToUpload.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
break;
case R.id.bDownloadImage:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
imageToUpload.setImageURI(selectedImage);
}
}
private class UploadImage extends AsyncTask<Void, Void, Void>{
Bitmap image;
String name;
public UploadImage(Bitmap image, String name){
this.image = image;
this.name = name;
}
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
ContentValues dataToSend = new ContentValues();
dataToSend.put("image", encodedImage);
dataToSend.put("name", name);
try{
URL url = new URL(SERVER_ADDRESS);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(String.valueOf(dataToSend));
writer.flush();
writer.close();
os.close();
connection.connect();
is = connection.getInputStream();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
}
}
}
这是我的 php 代码:
<?php
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$image = isset($_POST["image"]) ? $_POST["image"] : '';
$decodedImage = base64_decode("$image");
file_put_contents("pictures/" + $name + ".JPG", $decodedImage);
?>
运行应用程序并选择照片后,没有出现任何错误,而且似乎可以正常工作,但从服务器端看,没有上传照片。任何帮助将不胜感激。
【问题讨论】:
-
您没有发送请求中的数据。这就是为什么您无法在服务器端接收任何内容的原因。我个人会推荐使用像
OkHttp这样的库来完成与网络相关的任务。它很容易使用。更多信息:square.github.io/okhttp -
你说得对,我更新了代码以反映我现在正在尝试做的事情
-
您只是发送图像,没有任何服务器密钥。而且你的php代码不符合它。写
writer.write(String.valueOf(dataToSend));而不是writer.write(encodedImage); -
最新发生的事情似乎在 String.valueOf(dataToSend)
object has been collected Cannot evaluate android.ContentValues.toString()中,特别是在“名称”键:值对上
标签: php android apache upload httpurlconnection