2014-03-12 2 views
1

사진 desert.png을 Android 서버에서 PHP 서버로 업로드하려고하는데 저에게 적합하지 않으며 다음과 같은 오류 로그가 표시됩니다 : E/오류 : (1450) : /desert.png : 열기 실패 : EROFS (읽기 전용 파일 시스템) 이 내 코드입니다 :이미지를 android에서 PHP 서버로 업로드

/** 
* Background Async Task to upload file 
* */ 
class UploadFileToURL extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread 
    * Show Progress Bar Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(progress_bar_type); 
    } 

    /** 
    * Uploading file in background thread 
    * */ 
    @Override 
    protected String doInBackground(String... f_url) { 
     int count; 
     try { 
      URL url = new URL(f_url[0]); 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int lenghtOfFile = conection.getContentLength(); 
      Log.i("lenghtOfFile",lenghtOfFile+""); 
      // download the file 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 
      Log.i("InputStream","InputStream"); 
      // Output stream 
      OutputStream output = new FileOutputStream("desert.png"); 
      Log.i("OutputStream","OutputStream"); 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.desert);   
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
      byte [] byte_arr = stream.toByteArray(); 

      byte data[] = new byte[1024]; 

      long sentByte = 0; 
      while ((count = input.read(byte_arr)) != -1) { 
       output.write(byte_arr, 0, count); 
       sentByte += count; 


       // passed=passedTime -previosTime ; 
       // previosTime = passed ; 

       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress(""+(int)((sentByte*100)/lenghtOfFile)); 
       Log.i("log_lenghtOfFile",lenghtOfFile+""); 
       Log.i("log_total",sentByte+""); 
       Log.i("log_ourcentage",(int)((sentByte*100)/lenghtOfFile)+""); 

       // Log.i("log_Debit",passedTime +""); 
       // writing data to file 
       output.write(data, 0, count); 
      } 
      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 



     return null; 
    } 

    /** 
    * Updating progress bar 
    * */ 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     pDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    /** 
    * After completing background task 
    * Dismiss the progress dialog 
    * **/ 
    @Override 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after the file was downloaded  
     dismissDialog(progress_bar_type); 

     // Displaying downloaded image into image view 
     // Reading image path from sdcard 
    // String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; 
     // setting downloaded into image view 
    // my_image.setImageDrawable(Drawable.createFromPath(imagePath)); 
    } 

} 
+0

당신이'manifest.xml'에'READ_PERMISSION'을 추가했는지 확인하십시오. –

+0

나는 그것을 추가했지만 여전히 같은 문제가있다. 내 코드에 대해 어떻게 생각 하나? – victoria

+1

UploadFileToUrl() 코드 – Pankaj

답변

0

하면 AndroidExample .Another 좋은 튜토리얼 here에서이 링크를 따를 수있을 수 있습니다 . 코드에서 무서워하지 마라. 이미지를 업로드하기 위해 따라야 할 몇 가지 규칙이있다.

+0

첫 번째 링크의 코드를 시도했지만 항상 오류가 있습니다. 소스 파일이 없습니다. /mnt/sdcard/desert.png, 나는/mnt/sdcard/아래의 그림을 DDMS에 넣었습니다. 에뮬레이터를 다시 시작하지만 여전히 동일한 메시지입니다. – victoria

+1

이제 Git 저장소 (https : //github.com/pankajgangwar/android)에서 전체 소스 코드를 다운로드 할 수 있습니다. 갤러리에서 사진을 선택하고 서버에 업로드하십시오 - @ victoria – Pankaj

+0

문제는 해결되었습니다. AndroidGeek :)하지만이 링크에 또 다른 문제가 있습니다. [link] (http://stackoverflow.com/questions/22377470/android-4-3-httpurlconnection-no-authentication-challenges-found) – victoria

관련 문제