我是一名 .net 开发人员,过去 3-4 年一直在 Visual Studio 工作,但现在我的老板让我创建一些 Android 应用程序,所以我必须回到我的大学时光并练习我的 Java 技能(没那么好)。现在,该应用程序非常简单:将图像上传到服务器,从 Web 应用程序查看该图像等。
现在我的问题是:我已经阅读了很多关于如何做到这一点并尝试了很多教程,我终于找到了一个好的教程。它有一个我修改的上传类,但我不断收到此错误:
#Description Resource Path Location Type Can only iterate over an array or an instance of java.lang.Iterable#
老实说,我完全不知道该怎么做;我搜索了这个并找到了几个帖子,但没有一个能真正帮助我解决这个问题。异常显示在这一行:for (String sdPath : path)
。这是代码:
public class HttpUploader extends AsyncTask<String, Void, String> {
protected String doInBackground(String path) {
String outPut = null;
for (String sdPath : path) {
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
//Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
int lol=0;
String ba1 = Base64.encodeToString(ba,lol);
System.out.println("uploading image now " + ba1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://path to the image upload .php file/api.upload.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
}
catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
}
return outPut;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
}