Uploading image from Android to GCS
Since no one is answering this question, let me update the way I solved this problem. I ended up following this https://github.com/pliablematter/simple-cloud-storage project.
I could upload Pictures/Videos to GCS from my Android app.
Send Images/Videos from Android/iOS to Google Cloud Endpoints
As Saiyr suggested, you should upload directly to the Cloud Storage from the Android application (without going through Endpoints). See Uploading image from Android to GCS and upload image to google cloud storage with an application android.
Since you know the bucket and file name, the URL of the uploaded file is easily deductible, see the following documentation to decide which URL to use (depending on whether the user shall be authenticated or not, see Section "A user is granted read access to an object") https://cloud.google.com/storage/docs/cloud-console#_accessing
uploading image Android to GCS using PHP
Found the problem ... it was a on "s" that i forgot at the end of "uploaded_files" in my android side
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_files\";filename=\""
+ selectedFilePath + "\"" + lineEnd);
upload image to google cloud storage with an application android
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
Related Topics
"Unknown Bits Set in Runtime_Flags: 0X8000" Warning in Logcat on Android Q Emulator
Getting Header from Response (Retrofit/Okhttp Client)
Asynctask, Must It Take Such a Performance Penalty Hit...
How to Detect Bluetooth State Change Using a Broadcast Receiver
Unexpected Namespace Prefix "Xmlns" Found for Tag Linearlayout
How to Check If Running on UI Thread in Android
How to Set Searchview Textsize
Android Sharedpreferences , How to Save a Simple Int Variable
No Notification Sound When Sending Notification from Firebase in Android
Rendering Problems in Android Studio V 1.1/1.2
How to Force an Intentservice to Stop Immediately with a Cancel Button from an Activity
How to Add .So Files to an Android Library Project Using Gradle 0.7+
Android Test Code Coverage with Jacoco Gradle Plugin
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
Android/Eclipse Panic: Could Not Open
Newer Versions of Android Studio and Only Two Drawable Directory - Drawable and Drawable-V21