[英]upload image to google cloud storage with an application android
我正在嘗試將圖像上傳到谷歌雲存儲。
我找到了一個像https://github.com/pliablematter/simple-cloud-storage這樣的例子,它解釋了如何做到這一點。
我創建了一個項目存儲桶等,但是當我嘗試創建客戶端 ID 時出現此錯誤:
發生了錯誤。 請稍后重試
這個錯誤從何而來?
也許還有另一種方法可以使用 android 應用程序將文件上傳到谷歌雲存儲?
在新控制台中編輯 ____________ 我可以看到一條消息,告訴我只有項目所有者才能為應用程序 Web 和帳戶服務創建客戶端。 所以錯誤是因為我與帳戶合作者連接
編輯 __________ 現在我可以創建客戶端 ID,但我不知道如何將文件從 android 上傳到存儲桶,我讀了這個https://github.com/pliablematter/simple-cloud-storage但它適用於 java 而不是 Android ,任何人都有一個例子,我該怎么做?
任何幫助將不勝感激
我終於可以像這樣在谷歌存儲上上傳圖片了
class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected String doInBackground(Void... params) {
try {
List<String> scopes = new ArrayList<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
httpTransport= new com.google.api.client.http.javanet.NetHttpTransport();
//agarro la key y la convierto en un file
AssetManager am = getAssets();
String STORAGE_SCOPE = "https://www.google.com/analytics/feeds/" ;
InputStream inputStream = am.open("*********114db0.p12"); //you should not put the key in assets in prod version.
//convert key into class File. from inputstream to file. in an aux class.
File file =stream2file(inputStream);
//Google Credentianls
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("**********ic1bgevf3h@developer.gserviceaccount.com")
.setServiceAccountScopes((scopes))
.setServiceAccountPrivateKeyFromP12File(file)
.build();
String URI = "https://storage.googleapis.com/" + "BUCKET_NAME"+"/"+"zzzzz3"+".jpg";
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
//byte array holds the data, in this case the image i want to upload in bytes.
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.camera);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
HttpContent contentsend = new ByteArrayContent("image/jpeg", bitMapData );
HttpRequest putRequest;
putRequest = requestFactory.buildPutRequest(url, contentsend);
com.google.api.client.http.HttpResponse response = putRequest.execute();
String content = response.parseAsString();
Log.d("debug", "response is:"+response.getStatusCode());
Log.d("debug", "response content is:"+content);
} catch (IOException | GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
protected void onPostExecute(String feed) {
int i = 0;
int j = i;
// TODO: check this.exception
// TODO: do something with the feed
}
}
不要忘記下載 jar 並將其放在 lib 文件夾中: - com.fasterxml.jackson.core.jar - google-api-client-1.20.0.jar - google-api-services-storage-v1beta2-rev21-1.15 .0-rc.jar - google-http-client-1.20.0.jar - google-http-client-jackson2-1.20.0.jar - google-http-client-jdo-1.20.0.jar - google-oauth -client-1.20.0.jar
public class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected String doInBackground(Void... params) {
JsonFactory jsonFactory = new JacksonFactory();
try {
List<String> scopes = new ArrayList<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
//convert key into class File. from inputstream to file. in an aux class.
File file =getTempPkc12File();
//Google Credentianls
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("xxxxxx-xxxx-xxx@xxxxx-xxxxxx.iam.gserviceaccount.com")
.setServiceAccountScopes((scopes))
.setServiceAccountPrivateKeyFromP12File(file)
.build();
String URI = "https://storage.googleapis.com/" + GOOGLE_STORAGE_BUCKET+"/images/"+createImageFile();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
//byte array holds the data, in this case the image i want to upload in bytes.
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.camera);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
HttpContent contentsend = new ByteArrayContent("image/jpeg", bitMapData );
HttpRequest putRequest;
putRequest = requestFactory.buildPutRequest(url, contentsend);
com.google.api.client.http.HttpResponse response = putRequest.execute();
String content = response.parseAsString();
Log.i("debug", "response is:"+response.getStatusCode());
Log.i("debug", "response content is:"+content);
} catch (IOException | GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("ErrorUpload",""+e.toString());
}
return "";
}
protected void onPostExecute(String feed) {
int i = 0;
int j = i;
// TODO: check this.exception
// TODO: do something with the feed
Log.i("OnPost",""+feed);
}
}
private File getTempPkc12File() throws IOException {
// xxx.p12 export from google API console
InputStream pkc12Stream = (NewProduceInfo.this).getResources().getAssets().open("xxxxx-xxxxxxx-xxxxxx.p12");
File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
OutputStream tempFileStream = new FileOutputStream(tempPkc12File);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = pkc12Stream.read(bytes)) != -1) {
tempFileStream.write(bytes, 0, read);
}
return tempPkc12File;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.