从 SD 卡获取图像,压缩它,然后将其保存回 SD?

发布于 2024-12-03 12:48:31 字数 260 浏览 1 评论 0原文

我正在尝试从相机捕获图像,如果图像太大,我想压缩位图并将其发送回 SD 卡。我最初尝试将图像直接拉入内部存储器,但显然根据我之前问题的答案我可以做到这一点: Android 照片以非常小的尺寸拍摄

我怎样才能将该图像文件带回到我的应用程序内存中?

Im trying to capture an image from the camera, and if its too big i want to compress the bitmap and send it back to the sd card. I initially tried to pull the image directly into internal memory but apparently according to this answer to my previous question i can do that: android picture from camera being taken at a really small size

How could I bring that image file back into my apps internal memory?

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

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

发布评论

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

评论(3

月下伊人醉 2024-12-10 12:48:31

您可以使用下面的代码重新缩放图像。

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

这会将您的图像压缩为更小的尺寸,您只需要根据您的要求提供新的高度和宽度。

You may rescale image using the code below.

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

This will compress your image to smaller size, you just have need to give new height and width as per your requirement.

清音悠歌 2024-12-10 12:48:31

单击按钮调用方法将图像保存到SD卡:

SaveImage(bitmap);

将图像保存到Sd卡:

private void SaveImage(Bitmap finalBitmap) {

  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/demo_images");    
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
// Below code will give you full path in TextView

> txtPath1.setText(file.getAbsolutePath());

  if (file.exists ()) file.delete (); 
  try {
         FileOutputStream out = new FileOutputStream(file);
         finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
         out.flush();
         out.close();

  } catch (Exception e) {
         e.printStackTrace();
  }
}

从Sd卡获取图像:

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);

用于存储图像到内部存储器:

从内部保存和读取位图/图像Android 中的内存

Onclick of button Call Method to Save your Image to SDcard:

SaveImage(bitmap);

Saving Image to SdCard:

private void SaveImage(Bitmap finalBitmap) {

  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/demo_images");    
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
// Below code will give you full path in TextView

> txtPath1.setText(file.getAbsolutePath());

  if (file.exists ()) file.delete (); 
  try {
         FileOutputStream out = new FileOutputStream(file);
         finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
         out.flush();
         out.close();

  } catch (Exception e) {
         e.printStackTrace();
  }
}

For getting Image from SdCard:

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview.setImageDrawable(bitmap);

For Storing Image to Internal Memory:

Saving and Reading Bitmaps/Images from Internal memory in Android

往日情怀 2024-12-10 12:48:31

从相机获得的图像已经被压缩为 jpeg 格式。您永远不会从相机获得原始图像。

The image that you get from the camera is already compressed in the jpeg format. You will never get a raw image from the camera.

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