用户可以通过相机在android端拍摄5-6张照片。所以,我使用了ACTION_IMAGE_CAPTURE。在 onActivityResult 中,我这样做是为了收集相机拍摄的图像的位图。假设第一张照片和第二张照片如下。
if(requestCode == 1)
{
bitMap1 = (Bitmap)extras.get("data");
imageView1.setImageBitmap(bitMap1);
globalvar = 2;
}
if(requestCode == 2)
{
bitMap1 = (Bitmap)extras.get("data");
imageView2.setImageBitmap(bitMap2);
globalvar = 2;
}
要将这些图像发送到 php 服务器,我执行以下操作..
protected String doInBackground(Integer... args) {
// Building Parameters
ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
bitMap1.compress(Bitmap.CompressFormat.JPEG, 90, bao1);
byte [] bytearray1 = bao1.toByteArray();
String stringba1 = Base64.encode(bytearray1);
ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
bitMap2.compress(Bitmap.CompressFormat.JPEG, 90, bao2);
byte [] bytearray2 = bao2.toByteArray();
String stringba2 = Base64.encode(bytearray2);
String parameter1 = "tenant";
String parameter2 = "price";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("person",parameter1));
params.add(new BasicNameValuePair("price",parameter2));
params.add(new BasicNameValuePair("image1",stringba1));
params.add(new BasicNameValuePair("image2",stringba2));
JSONObject json = jParser.makeHttpRequest(requiredurl, "POST", params);
Log.d("Details", json.toString());
int success = json.getInt("connected");
if (success == 1) {
//blah blah
}
}
这是makeHttpRequest()方法:
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
................
....................... // Here the result is extracted and made it to json object
.............................
// return JSON
return jObj; // returning the json object to the method that calls.
}
下面是 php 代码 fragment :
$name = $_POST[person];
$price = $_POST[price];
$binary1 = base64_decode($image2);
$binary2 = base64_decode($image2);
$file1 = tempnam($uploadPath, 'image2');
$fp1 = fopen($file1, 'wb');
fwrite($fp1, $binary1);
fclose($fp1);
.................
............................
但是我无法将图像存储在服务器端文件夹中。甚至我在一些链接中看到说 Base64 在上传多个图像时不是更好的方式。有人可以建议我如何继续吗?见过this和许多其他链接,但无法了解如何继续满足我的要求,因为我什至必须随图像一起发送一些数据(如人名、价格)。非常感谢对此的任何帮助。
注意:即使有人可以建议我如何将上述临时文件($file1)保存在服务器文件夹中,我也会非常感激。
最佳答案
您是否考虑过使用 FTP 来代替目前的方法? Apache 有一个名为 Apache's commons-net-ftp 库的库可以轻松完成这项工作。
这是我很久以前在 stackoverflow 上问过的一个问题。我之前实现了几乎相同的代码,但我发现 FTP 方法更容易将文件上传到服务器。
@TemporaryNickName 如何将其他数据与该图像一起发送。此外,我的图像数据是位图,而不是 uri。根据我现在的事情如何实现?
*有很多教程向您展示如何将位图转换为图像文件(这几乎是临时保存文件并在 FTP 完成后立即删除的方法),此外,当您使用默认内置相机应用程序拍照时,您的图像会自动保存。 那么发送数据应该单独完成,在这种情况下,我会用 $_POST 编写一个 PHP 脚本来接收数据,而不是将其保存到数据库或写入 XML*
要保存上传的文件,请使用
move_uploaded_file($src, $path);
关于php - 上传多张图片到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14567050/