Upload images to Google Cloud Storage using the app android
I’m trying to upload an image to Google cloud storage.
I found an example of https://github.com/pliablematter/simple-cloud-storage This explains how to do this.
I created an artifact bucket etc, but when I tried to create a client ID
I’m getting this error:
An error has occurred. Please try again later< img alt="Enter image description here" src="http://res.cloudinary.com/dfrkaf37y/image/upload/v1671549032/etbye/o9Jl8.png"/>
Where did this error come from?
Maybe there is another way to upload files to Google Cloud Storage using Android apps?
> edit ____________ In the new console, I can see a message telling me that only project owners can create clients for the Application Web and Account services.
So the error is because I connected an account collaborator
> edit __________
Now I can create a client ID, but I don’t know how to upload a file from android to a bucket, I read this https://github.com/pliablematter/simple-cloud-storage But it works on java and not Android, anyone has an example, how do I do that?
Any help would be appreciated
Solution
I can finally upload images to 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("**********[email protected]")
.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 the jar and put it in the 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