private void uploadToPhotos() throws FileNotFoundException { Log.e(TAG, "uploadToPhotos: ");
asyncHttpClient.setTimeout(50 * 1000);
String token = sharedPreferences.getString(Constants.ACCESS_TOKEN,"");
String url = "https://photoslibrary.googleapis.com/v1/uploads";
/*StringEntity jsonEntity = null;
JSONObject jsonParams = new JSONObject();*/
File file = new File(path);
/* Bitmap src= BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
src.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
try {
jsonParams.put("media-binary-data", data);
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonEntity = new StringEntity(jsonParams.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/
RequestParams params = new RequestParams();
params.put("media-binary-data", file);
Log.e(TAG, "uploadToPhotos:MIME "+Constants.getMimeType(path));
Constants.enableSSL(asyncHttpClient);
asyncHttpClient.addHeader("Content-type","application/octet-stream");
asyncHttpClient.addHeader("X-Goog-Upload-Content-Type",Constants.getMimeType(path));
asyncHttpClient.addHeader("X-Goog-Upload-Protocol","raw");
asyncHttpClient.addHeader("Authorization","Bearer "+token+"");
asyncHttpClient.post(SingleImageViewScreen.this,url, params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String res = new String(responseBody);
Log.e(TAG, "onSuccess: "+res );
createNewPhoto(res);
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
try {
String res = new String(responseBody);
Log.e(TAG, "onFailed: "+res );
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
private void createNewPhoto(String utoken) {
Log.e(TAG, "uploadToPhotos: ");
asyncHttpClient.setTimeout(15 * 1000);
String token = sharedPreferences.getString(Constants.ACCESS_TOKEN,"");
String url = "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate";
StringEntity jsonEntity = null;
// jsonParams.put("")
try {
String body = "{ \"newMediaItems\": [\n" +
" {\n" +
" \"description\": \"test image\",\n" +
" \"simpleMediaItem\": {\n" +
" \"uploadToken\": \"" + utoken +
"\" }\n" +
" }\n" +
" " +
" ]\n" +
"}";
Log.e(TAG, "createNewPhoto: "+body);
jsonEntity = new StringEntity(body);
} catch (Exception e) {
e.printStackTrace();
}
// Log.e(TAG, "uploadToPhotos:MIME "+Constants.getMimeType(path));
Constants.enableSSL(asyncHttpClient);
asyncHttpClient.addHeader("Content-type","application/json");
asyncHttpClient.addHeader("Authorization","Bearer "+token+"");
asyncHttpClient.post(SingleImageViewScreen.this,url, jsonEntity, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String res = new String(responseBody);
Log.e(TAG, "onSuccess: "+res );
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
try {
String res = new String(responseBody);
Log.e(TAG, "onFailed: "+res );
}
catch (Exception e){
e.printStackTrace();
}
}
});
}
I am trying to upload photo to Google photos but receiving error "code: 3, Failed: There was an error while trying to create this media item." Can someone please help me to figure out the issue.
I am trying to upload photo to Google photos but receiving error "code: 3, Failed: There was an error while trying to create this media item." Can someone please help me to figure out the issue.