Android:活动结果后将图片保存到 SD 卡时出现问题

发布于 2024-11-16 09:43:16 字数 833 浏览 7 评论 0原文

我的程序有两个问题: 1.拍照后,这就是我的onActivityResult():

        pictureTaken = (Bitmap) data.getExtras().get("data");
        ImageView im = (ImageView) findViewById(R.id.view);
        im.setImageBitmap(pictureTaken);

这只显示缩略图(显示时质量很差),当我将其保存到SD卡时,它也是一个小图像。我需要的是以全分辨率/质量保存/显示它。我要改变什么才能实现这个目标?

  1. 检索并显示用户选择的图像:

    selectedImage = data.getData();
    ImageView im = (ImageView) findViewById(R.id.view);
    im.setImageURI(selectedImage);
    

但是当保存用户选择的图像时,它在这里崩溃(由调试器发现):

...
File externalStorageFile = new File(dir, finalName);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//Error
resourceImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte b[] = bytes.toByteArray();
try {
...

此外,位图是在 Android 中保存图像的唯一方法?

I have two problems in my program:
1.After taking a picture, this is what I have for onActivityResult():

        pictureTaken = (Bitmap) data.getExtras().get("data");
        ImageView im = (ImageView) findViewById(R.id.view);
        im.setImageBitmap(pictureTaken);

This displays only a thumbnail (bad quality when displayed), and when I save it to the SD card, it is also a small image. What I need is to save/display it in it's full resolution/quality. What do I change to achieve this?

  1. Retrieving and displaying the image selected by the user works:

    selectedImage = data.getData();
    ImageView im = (ImageView) findViewById(R.id.view);
    im.setImageURI(selectedImage);
    

But when saving the image the user picked, it crashes here (found by debugger):

...
File externalStorageFile = new File(dir, finalName);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//Error
resourceImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte b[] = bytes.toByteArray();
try {
...

Also, is bitmap the only way you can save images in Android?

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

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

发布评论

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

评论(2

谈场末日恋爱 2024-11-23 09:43:16

检查此链接以获取示例。如果从图库中选择的图像显示位图超出范围异常,您需要解码图像。

将图像缩放到屏幕尺寸....

<一个href="https://stackoverflow.com/questions/6123557/how-to-upload-images-from-sd-card-by-picking-over-them-in-android/6124781#6124781">从图库中挑选图像并解码它......

Check this Links for Sample. If the Selected images from Gallery shows Bitmap out of range exception you need to Decode image.

Scale Image to screen Size....

Picking Image from gallery and Decode it.......

情话难免假 2024-11-23 09:43:16

要将图像保存在 sdcard 中,请使用以下代码

    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File file = new File(extStorageDirectory, imagename);
    file.createNewFile();
    FileOutputStream fos = new  FileOutputStream(file);
    bm.compress(CompressFormat.JPEG, 75, fos);
    fos.flush();
    fos.close();

For saving the image in sdcard use the below code

    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File file = new File(extStorageDirectory, imagename);
    file.createNewFile();
    FileOutputStream fos = new  FileOutputStream(file);
    bm.compress(CompressFormat.JPEG, 75, fos);
    fos.flush();
    fos.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文