How to Transform a Bitmap into java.io.file to send to Amazon S3

Asked

Viewed 196 times

3

  • 1

    If your problem is send bitmap, because it posted the code to catch him? And what mistake you get?

  • I left there in case there is any error in the way to catch it, and so there is a better understanding of the steps I am following and just do not have a mistake because I do not know how to send a bitmap to Amazon S3

  • It doesn’t matter how you get the bitmap. But what problem are you facing when sending it? Show the code and error.

  • You will probably have to create a webservice

  • But using Amazon’s sdk it no longer makes the direct connection when it creates a connection client?

  • What I noticed, I have to pass a File instead of a Bitmap but how to do it to send?

Show 1 more comment

1 answer

3


I also use the Amazon SDK to upload to S3 and record the Bitmap in an app cache folder and then move to the SDK.

Something like:

@NonNull
public static File storeOnCache(Context context, Bitmap bitmap) throws IOException {
    File cacheDir = context.getCacheDir();
    File file = new File(cacheDir, generateRandomFilename("jpg"));

    FileOutputStream out = new FileOutputStream(file);

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

    out.flush();
    out.close();

    return file;
}

Where the generateRandomFileName is a simple method that generates a name for not having conflict.

@NonNull
public static String generateRandomFilename(@Nullable String extension) {
    return new StringBuilder(50)
            .append(System.currentTimeMillis())
            .append((int) (Math.random() * 10000.0))
            .append(".")
            .append(extension)
            .toString();
}

Ai may be worth having a later treatment to remove the file at the end of the upload.

Using:

Bitmap bitmap = ...

File bitmapOnCache = storeOnCache(this, bitmap);
String fileName = bitmapOnCache.getName();

// Dependendo da forma como irá fazer:

ObjectMetadata metadata = new ObjectMetadata();

metadata.setContentLength(mTotalFileBytes = bitmapOnCache.length());
metadata.setContentType("image/".concat(getExtension(fileName)));

PutObjectRequest por = new PutObjectRequest("BUCKET_NAME", fileName, new FileInputStream(bitmapOnCache), metadata);

AmazonS3Client client = ...

client.putObject(por);
  • What you pass as parameter to "bitmap = ..."?

  • There would be the Bitmap that you got from somewhere and that you need to send to S3

  • The next thing you know AmazonClientException: Unable to execute HTTP request: Write error: ssl=0xb7729c70: I/O error during system call, Connection reset by peer, is resetting the image size or pattern?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.