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

发布于 2024-12-11 11:50:18 字数 930 浏览 0 评论 0原文

我正在尝试从 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();

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

I am trying to download a set of .jpg's from Amazon S3 and store them to the internal storage (so they can't be copied by a malicious user). I have gotten this far but now I am stuck. I have found multiple questions that deal with bitmaps or arrays but nothing about storing an image and then accessing it later. Anyone know where I go from here?

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();

I have looked here in the dev guide and it gives the following code
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();

But this is for storing a string, not an image...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-12-18 11:50:19

您必须将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();

You have to copy InputStream to OutputStream. Something like this:

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();
末蓝 2024-12-18 11:50:19

你可以做类似下面的事情。

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());

You could do something like the following.

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文