2013-05-07 2 views
0

저는 .net 개발자이며 지난 3-4 년 동안 Visual Studio에서 일해 왔지만 지금은 내 보스입니다. 일부 Android 앱을 만들도록 요청했기 때문에 대학 시간으로 돌아가서 자바 기술을 연습해야합니다. 이제 앱은 간단합니다. 이미지를 서버에 업로드하고 웹 앱 등에서 해당 이미지를 봅니다.유형 배열 또는 java.lang.Iterable의 인스턴스를 반복 할 수 있습니다. 이미지 업로드 android

이제 내 질문 : 저는이 작업을 수행하고 많은 자습서를 시도하는 방법에 대해 많이 읽었습니다. 나는 마침내 좋은 것을 찾았다. 내가 개질 업로드 클래스를 가지고 있지만,이 오류가 점점 계속 :

#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; 
    } 
} 
+0

String#charAt([place])를 호출? 이 문제와 관련하여 더 이상의 질문이 있으십니까? –

답변

0

Java에서는 컴파일 타임 오류가 발생하는 이유 때문에 문자열을 반복 할 수 없습니다.

doInBackground() 방법 정의는 오버라이드 (override)되는 메소드가 사용 정확히 어느 가변 인자 (생략)

protected String doInBackground(String... path) { 

를 사용해야합니다. varargs는 액세스 할 때 배열과 동일합니다.

또한 컴파일러 경고를 피하기 위해 AsyncTask을 매개 변수화해야합니다. 당신이 (그 문자를 통해) 한 문자열을 반복 할 않은 경우

, 당신은 할 수 : 내 대답은 문제를 해결했다

for (char c: path.toCharArray()){ 
} 

또는 루프에 대한 간단한와