使用应用程序 android 将图像上传到谷歌云存储 [英] upload image to google cloud storage with an application android
问题描述
我正在尝试将图片上传到谷歌云存储.
我找到了一个这样的例子 但它适用于 java 而不是 Android,任何人都有一个例子,我该怎么做?
任何帮助将不胜感激
我终于可以像这样在 google storage 上上传图片了
class RetrieveFeedTask extends AsyncTask{私有异常异常;受保护的字符串 doInBackground(Void... params) {尝试 {列表<字符串>scopes = new ArrayList();scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);httpTransport= 新的 com.google.api.client.http.javanet.NetHttpTransport();//agarro la key y la convierto en un fileAssetManager am = getAssets();String STORAGE_SCOPE = "https://www.google.com/analytics/feeds/" ;InputStream inputStream = am.open("*********114db0.p12");//您不应该将密钥放在生产版本的资产中.//将密钥转换为类文件.从输入流到文件.在一个辅助类.文件文件 =stream2file(inputStream);//谷歌凭证GoogleCredential 凭证 = 新 GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY).setServiceAccountId("**********ic1bgevf3h@developer.gserviceaccount.com").setServiceAccountScopes((范围)).setServiceAccountPrivateKeyFromP12File(文件).建造();String URI = "https://storage.googleapis.com/" + "BUCKET_NAME"+"/"+"zzzzz3"+".jpg";HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);GenericUrl url = new GenericUrl(URI);//字节数组保存数据,在这种情况下,我想以字节为单位上传图像.资源 res = getResources();Drawable drawable = res.getDrawable(R.drawable.camera);位图位图 = ((BitmapDrawable)drawable).getBitmap();ByteArrayOutputStream 流 = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 100, 流);byte[] bitMapData = stream.toByteArray();HttpContent contentsend = new ByteArrayContent("image/jpeg", bitMapData );HttpRequest putRequest;putRequest = requestFactory.buildPutRequest(url, contentsend);com.google.api.client.http.HttpResponse 响应 = putRequest.execute();字符串内容 = response.parseAsString();Log.d("debug", "响应为:"+response.getStatusCode());Log.d("debug", "响应内容为:"+content);} catch (IOException | GeneralSecurityException e) {//TODO 自动生成的 catch 块e.printStackTrace();}返回 "";}protected void onPostExecute(String feed) {int i = 0;int j = i;//TODO: 检查 this.exception//TODO:对提要做一些事情}}
不要忘记下载 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
I'm trying to upload an image to google cloud storage.
I found an example like this https://github.com/pliablematter/simple-cloud-storage that explains how to do that.
I created a project bucket, etc., but when I try to create a client id
I get this error:
An error has occurred. Please retry later
Where did this error come from?
Maybe there is another method to upload file to the google cloud storage with an android application?
EDIT____________ in the new console I can see a message telling me only owner of the project can create clients for the application web and account services. So error is caused because I connect with a account collaborator
EDIT__________ Now I can create client id, but I don't know how I can upload file from android to bucket, I read this https://github.com/pliablematter/simple-cloud-storage but it is for java not Android, anyone has an example how can I do that ?
Any help will be appreciated
I can finally upload images on google storage like this
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
}
}
Don't forget to download jar and put it on lib folder: - 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
这篇关于使用应用程序 android 将图像上传到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!