Hi everyone.
I’m using Backendless.Files.uploadFromStream function and often get error BackendlessException{ code: ‘500’, message: ‘null’ }.
Her is inner response from connection.getErrorStream() in com.backendless.Files class
response = "Internal server error with id 2304D8B2-1484-B90F-FF06-01E71C02B700 "
Here is my upload code:
public class CompressedImageFileOutputStreamRouter extends IOutputStreamRouter {
private final File mFile;
private final int mMaxSize;
private final Bitmap.CompressFormat mCompressFormat;
private final int mQuality;
public CompressedImageFileOutputStreamRouter(final File file, final int maxSize, final Bitmap.CompressFormat compressFormat, final int quality) {
mFile = file;
mMaxSize = maxSize;
mCompressFormat = compressFormat;
mQuality = quality;
}
@Override
public void writeStream(final int i) throws IOException {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = createSampleSizeForImageSizes(mFile.getAbsolutePath(), mMaxSize, options);
boolean doCompressWork;
Bitmap bitmap = null;
do {
//noinspection ErrorNotRethrown
try {
bitmap = BitmapFactory.decodeFile(mFile.getAbsolutePath(), options);
doCompressWork = false;
} catch (final OutOfMemoryError ignored) {
options.inSampleSize *= 2;
doCompressWork = true;
}
} while (doCompressWork);
if (null == bitmap) {
throw new IOException("can't create image from file! ignore!");
}
if (bitmap.getWidth() > mMaxSize || bitmap.getHeight() > mMaxSize) {
bitmap = generateScaledBitmap(bitmap, mMaxSize);
}
/*
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(getOutputStream(), i);
bitmap.compress(mCompressFormat, mQuality, bufferedOutputStream);
bufferedOutputStream.flush();
*/
bitmap.compress(mCompressFormat, mQuality, getOutputStream());
bitmap.recycle();
}
private static Bitmap generateScaledBitmap(final Bitmap bitmap, final int maxSize) {
final float scale = Math.min(1f * maxSize / bitmap.getWidth(), 1f * maxSize / bitmap.getHeight());
try {
return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scale), Math.round(bitmap.getHeight() * scale), true);
} finally {
bitmap.recycle();
}
}
private static int createSampleSizeForImageSizes(final String filePath, final int maxSize, final BitmapFactory.Options options) {
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
final float sampleSize = Math.max(1f * options.outWidth / maxSize, 1f * options.outHeight / maxSize);
options.inJustDecodeBounds = false;
return Math.round(sampleSize);
}
}
final String fileName = // todo;
final BackendlessFile backendlessFile =
Backendless.Files
.uploadFromStream(new CompressedImageFileOutputStreamRouter(file, IMAGE_MAX_SIZE, Bitmap.CompressFormat.JPEG, 80), fileName,
filePath, true);
return backendlessFile.getFileURL();