序列化对象(jpg)到内部存储

时间:2011-10-22 14:39:05

标签: android serialization amazon-s3 amazon-web-services

我正在尝试从Amazon S3下载一组.jpg并将它们存储到内部存储中(因此恶意用户无法复制它们)。我已经走到了这一步,但现在我被卡住了。我发现了多个处理位图或数组的问题,但没有关于存储图像然后再访问它的问题。有谁知道我从哪里去?

String itemName = iconNames.getString(iconNames.getColumnIndexOrThrow(DbAdapter.KEY_ICON));
        itemName = itemName + ".jpg";
        GetObjectRequest getObject = new GetObjectRequest(bucket, itemName);

        S3Object icon = mS3Client.getObject(getObject);
        InputStream input = icon.getObjectContent();

我在开发指南中查看了它,它提供了以下代码 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

但这是用于存储字符串,而不是图像......

2 个答案:

答案 0 :(得分:0)

您必须将InputStream复制到OutputStream。像这样:

InputStream input = icon.getObjectContent();
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = input.read(buf)) > 0) {
    fos.write(buf, 0, len);
}
input.close();
fos.close();

答案 1 :(得分:0)

您可以执行以下操作。

Bitmap bitmapPicture = someBitmap
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "tmp.png");
fOut = new FileOutputStream(file);

bitmapPicture.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());