android 为位图命名并保存

发布于 2025-01-04 11:33:02 字数 184 浏览 4 评论 0原文

我有一个位图,想保存到 SD 卡上。我有两个问题:

  1. 为保存的文件设置一个名称
  2. 保存文件

我环顾 Stack Overflow,找不到任何有效的解决方案,对于这两个问题(主要是保存部分,设置名称部分让我很困惑)

有一个简单直接的解决方案吗?

谢谢!

I have a bitmap that I want to save to the sd card. I have 2 problems with that:

  1. Set a name to the saved file
  2. Save the file

I looked around Stack Overflow and couldn't find anything that worked, for both problems (mostly for the saving part, the set name part just got me all confused)

Is there a simple straight forward solution to this?

Thanks!

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

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

发布评论

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

评论(2

命硬 2025-01-11 11:33:02
Bitmap yourBitmap; // you have to get your bitmap into this variable
String filePath = "/mnt/sdcard/"; // some times it may be only /sdcard not /mnt/sdcard
filePath += "newFileName.jpg";

        try {
            yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

您必须在清单文件中使用以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Bitmap yourBitmap; // you have to get your bitmap into this variable
String filePath = "/mnt/sdcard/"; // some times it may be only /sdcard not /mnt/sdcard
filePath += "newFileName.jpg";

        try {
            yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

You have to use below permission in manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
万水千山粽是情ミ 2025-01-11 11:33:02

试试这个:

FileOutputStream out = new FileOutputStream(new File("/mnt/sdcard/pic.jpg"));
yourBitmap.compress(Bitmap.CompressFormat.JPG, 80, out);
out.close();

Try this:

FileOutputStream out = new FileOutputStream(new File("/mnt/sdcard/pic.jpg"));
yourBitmap.compress(Bitmap.CompressFormat.JPG, 80, out);
out.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文