2016-05-22 79 views
0

我正嘗試將圖像從Android上傳到雲端存儲。我正在關注如何使用JSON API將文件上傳到Google雲端存儲的this official guide。這裏是我的代碼嘗試將文件上傳到Google雲端存儲時出現FileNotFoundException

private class uploadImage extends AsyncTask<File, Void, String> { 

    File file = mPhotoFile; 

    private String delimiter = "--"; 

    @Override 
    protected void onPreExecute() { 
     Toast.makeText(getActivity(), mPhotoFile.getPath(), Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    protected String doInBackground(File... params) { 


     try { 
      URL url = new URL("https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key"); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      urlConnection.setRequestMethod("POST"); 
      urlConnection.setChunkedStreamingMode(0); 
      urlConnection.setRequestProperty("Content-Type", "image/jpeg"); 
      urlConnection.setRequestProperty("Content-Length", String.valueOf(mPhotoFile.getPath().getBytes().length)); 
      urlConnection.setRequestProperty("Authorization", "my_key"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoOutput(true); 


      OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); 
      InputStream responseStream = new BufferedInputStream(urlConnection.getInputStream()); 
      BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream)); 

      out.write(("Content-Type: image/jpeg\r\n").getBytes()); 
      out.write(("Content-Length: " + String.valueOf(mPhotoFile.getPath().getBytes().length)).getBytes()); 
      out.write("\r\n".getBytes()); 
      out.write(mPhotoFile.getPath().getBytes()); 
      out.write("\r\n".getBytes()); 

      String line = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 

      while((line = responseStreamReader.readLine()) != null) { 
       stringBuilder.append(line).append("\n"); 
      } 

      String response = stringBuilder.toString(); 

      Log.i("CloudStorage", response); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return e.getMessage(); 
     } 

     return "Everything was a success"; 
    } 


} 

我使用的公共API訪問方法和附加的API密鑰像the guide says I could鏈接請求授權

下面是我得到

05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err: java.io.FileNotFoundException:https://www.googleapis.com/upload/storage/v1/b/backend-images/o?uploadType=media&name=myObject?key=my_key 
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197) 
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:  at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:  at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) 
05-22 10:24:01.798 3747-4045/com.example.kid.uimockup W/System.err:  at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:636) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at com.example.kid.uimockup.HomeFragment$uploadImage.doInBackground(HomeFragment.java:605) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
05-22 10:24:01.803 3747-4045/com.example.kid.uimockup W/System.err:  at java.lang.Thread.run(Thread.java:818) 
錯誤

如果這是客戶端或服務器端的問題,我毫無頭緒

+0

在你的真實代碼中'?key = my_key'是否被分配了正確的值,或者它與此相同? – Yazan

+0

我編輯它,我有真正的鑰匙 – kidustiliksew

回答

1

我在做了一些更改後纔開始工作。我得到401未經授權的錯誤代碼,這意味着我沒有權限訪問存儲桶。

因此,我不附加查詢參數key=api_key,我附加access_token=auth_token來授權請求​​。

然後,我添加了allUsers權限給我的存儲桶(使其公開給大家寫和讀),它的工作。

相關問題