【问题标题】:Android: problems with saving picture to sd card after activity resultAndroid:活动结果后将图片保存到 SD 卡的问题
【发布时间】:2011-06-22 10:52:57
【问题描述】:

我的程序有两个问题: 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 中保存图像的唯一方法吗?

【问题讨论】:

  • 堆栈跟踪说明了什么?请发帖。

标签: android image android-camera android-sdcard image-size


【解决方案1】:

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

Scale Image to screen Size....

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

【讨论】:

  • 我得到一个 nullPointerException... 也找到了!是在尝试解码时
  • 位图 bImage = BitmapFactory.decodeFile(selectedImage.getPath());
  • 是的!感谢第二个链接,它帮助我解决了解码错误。至于第一个问题,我仍然需要更彻底地阅读链接,看看我能找到什么。另外,由于我的应用程序是蜂窝优化的,我应该如何设置位图 (bImage = Bitmap.createScaledBitmap(bImage, 500, 500, true);)? 500 看起来不错,但如果他们想将照片传输到他们的计算机,我不应该使用与“图库”应用相同/相似的大小吗?
  • 感谢您帮助我离完成我的应用程序更近了一步!
【解决方案2】:

要将图像保存在 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();

【讨论】:

  • 这部分我有,保存工作正常,只是 5mp 相机正在用我的代码返回一张 80X80 的照片。我不是以正确的方式“获取数据”吗?
猜你喜欢
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多