Hello,
I am developing an Android app using storj sdk where people can upload and download images.
I followed the documentation but the upload part takes always at least around 1.7/1.8 seconds per image and that is a lot of time for a responsive mobile application.
Does anyone have idea of how to speed up this process? Here is the code I have tried, all with same results.
- Solution present at GitHub - storj/uplink-android: Storj network Android library
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[] data = baos.toByteArray();
uplink = new Uplink(UplinkOption.tempDir(getCacheDir().getPath()));
long startTime = System.nanoTime();
try (Project project = uplink.openProject(access);
ObjectOutputStream out = project.uploadObject("demo-bucket", user.getUid()+"/"+nomeFile);
InputStream in = new ByteArrayInputStream(data)) {
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.commit();
long endtime = System.nanoTime();
Log.e("Measure", "------TASK took : " + ((endtime-startTime)/1000000)+ "mS\n");
Intent intent = new Intent(UploadActivity.this, MainActivity.class);
startActivity(intent);
} catch (IOException e) {
throw new RuntimeException(e);
}
- Write method all in once
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[] data = baos.toByteArray();
UploadTask uploadTask = new UploadTask(access, "demo-bucket",user.getUid()+"/"+nomeFile, getCacheDir().getPath(), getApplicationContext(), data);
uploadTask.execute();
Intent intent = new Intent(UploadActivity.this, MainActivity.class);
startActivity(intent);
- Using an async task like suggested at GitHub - storj/uplink-android: Storj network Android library
protected Exception doInBackground(Void... params) {
Uplink uplink = new Uplink(UplinkOption.tempDir(mTempDir));
try (Project project = uplink.openProject(mAccess);
InputStream in = new ByteArrayInputStream(data);
ObjectOutputStream out = project.uploadObject(mBucket, mObjectKey)) {
byte[] buffer = new byte[128 * 1024];
int len;
while ((len = in.read(buffer)) != -1) {
if (isCancelled()) {
// exiting the try-with-resource block without commit aborts the upload process
return null;
}
out.write(buffer, 0, len);
if (isCancelled()) {
// exiting the try-with-resource block without commit aborts the upload process
return null;
}
publishProgress((long) len);
}
out.commit();
} catch (StorjException | IOException e) {
// exiting the try-with-resource block without commit aborts the upload process
return e;
}
return null;
}
None of the above tries uploaded the images in acceptable time and I’m not talking about many megas, all of them weight between 0.5 and 1 mega.
Referring to the download time, with the following code it takes around 1 second to download each image (same sizes as above) and I would be happy to decrease this too.
try (Project project = uplink.openProject(access);
InputStream in = project.downloadObject("demo-bucket", user.getUid() + "/" + link);) {
ImageView imageView = ((ImageView) v.findViewById(R.id.singleImage));
imageView.setImageBitmap(BitmapFactory.decodeStream(in));
((LinearLayoutCompat) activity.findViewById(R.id.allImagesLinearLayout)).addView(v);
long endtime = System.nanoTime();
Log.e("Measure", "------TASK took : " + ((endtime-startTime)/1000000)+ "mS\n");
}
Any type of Suggestions?
Thanks