在android中将位图保存到SD卡而不压缩它

发布于 2024-09-27 03:16:09 字数 1166 浏览 3 评论 0原文

我正在使用 android 内置相机拍照,然后将同一张照片附加到电子邮件中,当我在 1.6 设备中测试此功能时,我可以命名内置相机拍摄的照片,但在 2.1 中,图片是有一个名称,即由设备给出的名称,

如何在 2.1 内置相机图像中给出用户定义的名称..

为了避免这个问题,我手动保存图像,但是当我尝试通过意图将图像返回为位图,然后将其保存到 SD 时使用压缩方法的卡

此方法处理内置相机的结果,

protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  File file = new File(Environment.getExternalStorageDirectory()
    + "/test.png");
  switch (requestCode)
  {
  case PHOTO_ACTION:
   if (resultCode == RESULT_CANCELED)
   {
     addPhoto = false;
     Toast.makeText(this, "Canceled ", Toast.LENGTH_LONG).show();
     break;
   } else if (resultCode == RESULT_OK)
   {
    Bundle b = data.getExtras();
    Bitmap bm = (Bitmap) b.get("data");

    FileOutputStream out;
    try
     {

     out = new FileOutputStream(file);
     bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
     scanPhoto(file.toString());
     out = null;
     addPhoto = true;
     } catch (Exception e)
      {
       e.printStackTrace();
       addPhoto = false;
      }

但是当我像这样存储时我得到两个图像。一个是系统给定的名称,另一个是我给定的名称。但是用户定义的图像分辨率较低,所以我的问题是如何以更高的分辨率保存位图而不压缩它.. 请帮我

I am using android inbuilt camera to take picture and then attaching the same picture to email, when i am testing this functionality in 1.6 device, i am able name the picture that to be taken by in built camera, but in 2.1, the picture is having a name i.e given by device,

How to give user defined name in 2.1 inbuilt camera images..

to avoid that problem i am saving the image manually but when i try to get the image back through intent as bitmap and then saving it to sd card using compress method

this methods handles result from inbuilt camera

protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  File file = new File(Environment.getExternalStorageDirectory()
    + "/test.png");
  switch (requestCode)
  {
  case PHOTO_ACTION:
   if (resultCode == RESULT_CANCELED)
   {
     addPhoto = false;
     Toast.makeText(this, "Canceled ", Toast.LENGTH_LONG).show();
     break;
   } else if (resultCode == RESULT_OK)
   {
    Bundle b = data.getExtras();
    Bitmap bm = (Bitmap) b.get("data");

    FileOutputStream out;
    try
     {

     out = new FileOutputStream(file);
     bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
     scanPhoto(file.toString());
     out = null;
     addPhoto = true;
     } catch (Exception e)
      {
       e.printStackTrace();
       addPhoto = false;
      }

but when i am storing like this i am getting two images. one with system given name and other with name given by me. but the image one which has user defined is having less resolution so i question is how to save the bitmap with more resolution with out compressing it ..
please help.... me

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

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

发布评论

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

评论(2

我早已燃尽 2024-10-04 03:16:09

如果您只想保存位图而不损失质量,请尝试使用 CompressFormat.PNG 而不是 JPEG,我以前见过有人遇到过这个问题。尝试更改:

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

和:

bm.compress(Bitmap.CompressFormat.PNG, 100, out);

看看是否有帮助。

If you just want to save the bitmap without losing quality try using CompressFormat.PNG instead of JPEG, I've seen people having that problem before. Try changing:

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

with:

bm.compress(Bitmap.CompressFormat.PNG, 100, out);

and see it it helps.

情归归情 2024-10-04 03:16:09

除了上面的 Rick 回答之外,请确保您向相机意图提供一个 URI,它可以在其中保存完整图像,否则它将在意图的数据参数中返回拇指图像。
所以它会像:

Intent photoPickerIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File("Cache directory","img.png"); //== where you want full size image
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imgFile));
startActivityForResult(photoPickerIntent, PickPhoto);

这是我犯的错误。

Apart from Rick answer above, make sure your are providing a URI to camera intent where it can save the full image, else it will return thumb image in data parameter of intent.
So it will be like:

Intent photoPickerIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imgFile = new File("Cache directory","img.png"); //== where you want full size image
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(imgFile));
startActivityForResult(photoPickerIntent, PickPhoto);

This was the error that I was doing.

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