Search in sources :

Example 1 with BackendlessFile

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;
    }
}
Also used : Intent(android.content.Intent) BackendlessFile(com.backendless.files.BackendlessFile) Uri(android.net.Uri) BackendlessFile(com.backendless.files.BackendlessFile) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 2 with BackendlessFile

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();
        }
    });
}
Also used : BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BackendlessFile(com.backendless.files.BackendlessFile) LinearLayout(android.widget.LinearLayout) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 3 with BackendlessFile

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));
            }
        }
    });
}
Also used : AsyncUploadMessage(com.backendless.async.message.AsyncUploadMessage) UploadCallback(com.backendless.async.callback.UploadCallback) AsyncMessage(com.backendless.async.message.AsyncMessage) FileOutputStreamRouter(com.backendless.files.router.FileOutputStreamRouter) BackendlessFile(com.backendless.files.BackendlessFile) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 4 with BackendlessFile

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);
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) Intent(android.content.Intent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BackendlessFile(com.backendless.files.BackendlessFile) File(java.io.File) BackendlessFile(com.backendless.files.BackendlessFile) BackendlessFault(com.backendless.exceptions.BackendlessFault)

Example 5 with BackendlessFile

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();
    }
}
Also used : BackendlessException(com.backendless.exceptions.BackendlessException) Scanner(java.util.Scanner) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) URL(java.net.URL) GUID(weborb.v3types.GUID) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BackendlessFile(com.backendless.files.BackendlessFile)

Aggregations

BackendlessFile (com.backendless.files.BackendlessFile)5 BackendlessFault (com.backendless.exceptions.BackendlessFault)4 Intent (android.content.Intent)2 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 LinearLayout (android.widget.LinearLayout)1 UploadCallback (com.backendless.async.callback.UploadCallback)1 AsyncMessage (com.backendless.async.message.AsyncMessage)1 AsyncUploadMessage (com.backendless.async.message.AsyncUploadMessage)1 BackendlessException (com.backendless.exceptions.BackendlessException)1 FileOutputStreamRouter (com.backendless.files.router.FileOutputStreamRouter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1