2016-05-22 155 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权限给我的存储桶(使其公开给大家写和读),它的工作。

相关问题