use of com.backendless.files.BackendlessFile in project Android-SDK by Backendless.
the class MakeChoiceActivity method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case Default.SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
File imageFile = new File(getRealPathFromURI(selectedImage));
final String filePath = imageFile.getPath();
progressDialog = ProgressDialog.show(MakeChoiceActivity.this, "", "Loading", true);
Backendless.Files.upload(imageFile, Default.DEFAULT_PATH_ROOT, new AsyncCallback<BackendlessFile>() {
@Override
public void handleResponse(BackendlessFile response) {
String photoBrowseUrl = response.getFileURL();
Intent intent = new Intent();
intent.putExtra(Default.PHOTO_BROWSE_URL, photoBrowseUrl);
intent.putExtra(Default.FILE_PATH, filePath);
setResult(Default.ADD_NEW_PHOTO_RESULT, intent);
progressDialog.cancel();
finish();
}
@Override
public void handleFault(BackendlessFault fault) {
progressDialog.cancel();
Toast.makeText(MakeChoiceActivity.this, fault.toString(), Toast.LENGTH_SHORT).show();
}
});
}
break;
case Default.MAKE_NEW_PHOTO:
String photoCameraUrl = imageReturnedIntent.getStringExtra(Default.PHOTO_CAMERA_URL);
String filePath = imageReturnedIntent.getStringExtra(Default.FILE_PATH);
Intent intent = new Intent();
intent.putExtra(Default.PHOTO_CAMERA_URL, photoCameraUrl);
intent.putExtra(Default.FILE_PATH, filePath);
setResult(Default.ADD_NEW_PHOTO_RESULT, intent);
finish();
break;
}
}
use of com.backendless.files.BackendlessFile in project Android-SDK by Backendless.
the class UploadingActivity method uploadBitmap.
private void uploadBitmap(Bitmap photo) {
LinearLayout imageContainer = (LinearLayout) findViewById(R.id.imageContainer);
Drawable drawable = new BitmapDrawable(photo);
drawable.setAlpha(50);
imageContainer.setBackgroundDrawable(drawable);
String name = UUID.randomUUID().toString() + ".png";
Backendless.Files.Android.upload(photo, Bitmap.CompressFormat.PNG, 100, name, Defaults.DEFAULT_PATH_ROOT, new AsyncCallback<BackendlessFile>() {
@Override
public void handleResponse(final BackendlessFile backendlessFile) {
handleSuccess(backendlessFile);
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Toast.makeText(UploadingActivity.this, backendlessFault.toString(), Toast.LENGTH_SHORT).show();
}
});
}
use of com.backendless.files.BackendlessFile in project Android-SDK by Backendless.
the class UploadFileAsyncTask method doInBackground.
private void doInBackground(final File file, final String path) {
final AsyncUploadMessage asyncUploadMessage = new AsyncUploadMessage(uploadCallback);
ThreadPoolService.getPoolExecutor().execute(new Runnable() {
@Override
public void run() {
try {
FileOutputStreamRouter fileOutputStreamRouter = new FileOutputStreamRouter(file, new UploadCallback() {
public void onProgressUpdate(Integer progress) {
asyncUploadMessage.setCurrentProgress(progress);
ResponseCarrier.getInstance().deliverMessage(asyncUploadMessage);
}
});
BackendlessFile result = Backendless.Files.uploadFromStream(fileOutputStreamRouter, file.getName(), path, overwrite);
ResponseCarrier.getInstance().deliverMessage(new AsyncMessage<BackendlessFile>(result, responder));
} catch (Exception e) {
ResponseCarrier.getInstance().deliverMessage(new AsyncMessage<BackendlessFile>(new BackendlessFault(e), responder));
}
}
});
}
use of com.backendless.files.BackendlessFile in project Android-SDK by Backendless.
the class MakePhotoActivity method onActivityResult.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Default.CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
String pictureName = "camera" + random.nextInt() + ".jpg";
Bitmap bitmap = (Bitmap) data.getExtras().get(Default.DATA_TAG);
Bitmap bitmapFile = bitmap;
mImage.setImageBitmap(bitmap);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapFile.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + pictureName);
final String filePath = file.getPath();
try {
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes.toByteArray());
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
progressDialog = ProgressDialog.show(MakePhotoActivity.this, "", "Loading", true);
Backendless.Files.Android.upload(bitmap, Bitmap.CompressFormat.JPEG, 100, pictureName, Default.DEFAULT_PATH_ROOT, new AsyncCallback<BackendlessFile>() {
@Override
public void handleResponse(BackendlessFile response) {
String photoCameraUrl = response.getFileURL();
Intent intent = new Intent();
intent.putExtra(Default.PHOTO_CAMERA_URL, photoCameraUrl);
intent.putExtra(Default.FILE_PATH, filePath);
setResult(Default.ADD_NEW_PHOTO_RESULT, intent);
progressDialog.cancel();
finish();
}
@Override
public void handleFault(BackendlessFault fault) {
progressDialog.cancel();
Toast.makeText(MakePhotoActivity.this, fault.toString(), Toast.LENGTH_SHORT).show();
}
});
} else {
mImage = (ImageView) findViewById(R.id.camera_image);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, Default.CAMERA_PIC_REQUEST);
}
}
use of com.backendless.files.BackendlessFile in project Android-SDK by Backendless.
the class Files method uploadFromStream.
public BackendlessFile uploadFromStream(OutputStreamRouter outputStreamRouter, String name, String path, boolean overwrite) throws Exception {
HttpURLConnection connection = null;
String CRLF = "\r\n";
String boundary = (new GUID()).toString();
try {
String urlStr = Backendless.getUrl() + '/' + Backendless.getApplicationId() + '/' + Backendless.getSecretKey() + "/files/" + encodeURL(path) + "/" + encodeURL(name);
if (overwrite)
urlStr = urlStr + "?" + OVERWRITE_PARAMETER_NAME + "=" + overwrite;
java.net.URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setChunkedStreamingMode(DEFAULT_CHUNK_SIZE);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
for (String key : HeadersManager.getInstance().getHeaders().keySet()) connection.addRequestProperty(key, HeadersManager.getInstance().getHeaders().get(key));
try (OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true)) {
writer.append("--").append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(name).append("\"").append(CRLF);
writer.append("Content-Type: application/octet-stream").append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
outputStreamRouter.writeStream(outputStream);
outputStream.flush();
writer.append(CRLF).flush();
writer.append("--").append(boundary).append("--").append(CRLF);
}
if (connection.getResponseCode() != 200) {
Scanner scanner = new Scanner(connection.getErrorStream());
scanner.useDelimiter("\\Z");
String response = scanner.next();
scanner.close();
Matcher matcher = SERVER_ERROR_PATTERN.matcher(response);
String message = null;
String code = null;
while (matcher.find()) {
message = matcher.group(MESSAGE_POSITION);
code = matcher.group(CODE_POSITION);
}
throw new BackendlessException(code == null ? String.valueOf(connection.getResponseCode()) : code, message);
} else {
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
String response = scanner.next();
scanner.close();
Matcher matcher = SERVER_RESULT_PATTERN.matcher(response);
String fileUrl = null;
while (matcher.find()) fileUrl = matcher.group(MESSAGE_POSITION);
return new BackendlessFile(fileUrl);
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(ExceptionMessage.FILE_UPLOAD_ERROR, e);
} catch (IOException e) {
throw new BackendlessException(ExceptionMessage.FILE_UPLOAD_ERROR, e.getMessage());
} finally {
if (connection != null)
connection.disconnect();
}
}
Aggregations