Android 1.5 和 Android 1.6 之间海量存储 API 发生了哪些变化

发布于 2024-09-24 17:56:57 字数 1389 浏览 1 评论 0原文

不久前我写了一些代码来将图像保存在SD卡上。现在我将 targetSDKVersion 添加到我的清单中,现在我的文件保存代码停止工作。

我可以通过从清单中删除 targetSdkVersion 来重现它,因为我的应用程序不会向 SD 卡写入任何内容。

Android 1.5 和 1.6 之间是否存在 API 更改,导致我无法写入 SD 卡?

File imageDirectory = new File(Environment.getExternalStorageDirectory()
                 .getAbsolutePath()
                 + File.separator
                 + FOLDER_NAME);

Log.d(ImageSaver.class.getSimpleName(), "SD Card status: "
            + Environment.getExternalStorageState());

if (!imageDirectory.exists()) {
   boolean created = imageDirectory.mkdir();
   Log.d(ImageSaver.class.getSimpleName(), "Created image directory "
                + imageDirectory + " " + created);
}
File imageFile = new File(imageDirectory.getAbsolutePath() + File.separator
                    + name + nameSuffix + FILE_ENDING);

bitmap.compress(Bitmap.CompressFormat.PNG, FULL_QUALITY,
         new FileOutputStream(imageFile));

这是将位图压缩到 SD 卡的测试代码。使用以下清单条目不起作用:

<uses-sdk
   android:minSdkVersion="3"
   android:targetSdkVersion="8" />

我收到以下异常:

09-27 11:35:58.689: 错误/ImageSaver(8672):找不到文件 09-27 11:35:58.689: 错误/ImageSaver(8672): java.io.FileNotFoundException: /sdcard/FOLDER/1285580158662.png

删除 targetSdkVersion 使其再次在所有平台上运行。

如何使代码在 targetSdkVersion 设置下运行?

I wrote some code to save images on the SD Card a while back. Now I added the targetSDKVersion to my manifest and now my file saving code ceased to work.

I can reproduce it through removing the targetSdkVersion from my manifest from that on my App won't write anything to the SD Card.

Is there an API change between Android 1.5 and 1.6 that prevents me from writing to the SD-Card?

File imageDirectory = new File(Environment.getExternalStorageDirectory()
                 .getAbsolutePath()
                 + File.separator
                 + FOLDER_NAME);

Log.d(ImageSaver.class.getSimpleName(), "SD Card status: "
            + Environment.getExternalStorageState());

if (!imageDirectory.exists()) {
   boolean created = imageDirectory.mkdir();
   Log.d(ImageSaver.class.getSimpleName(), "Created image directory "
                + imageDirectory + " " + created);
}
File imageFile = new File(imageDirectory.getAbsolutePath() + File.separator
                    + name + nameSuffix + FILE_ENDING);

bitmap.compress(Bitmap.CompressFormat.PNG, FULL_QUALITY,
         new FileOutputStream(imageFile));

This is the test code for compressing an bitmap to the SD Card. With the following manifest entry it does not work:

<uses-sdk
   android:minSdkVersion="3"
   android:targetSdkVersion="8" />

I get the following exception:

09-27 11:35:58.689:
ERROR/ImageSaver(8672): File not found
09-27 11:35:58.689:
ERROR/ImageSaver(8672):
java.io.FileNotFoundException:
/sdcard/FOLDER/1285580158662.png

Removing the targetSdkVersion makes it work on all platforms again.

How can I make the code run with the targetSdkVersion set?

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

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

发布评论

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

评论(1

赠我空喜 2024-10-01 17:56:57

您必须添加权限 WRITE_EXTERNAL_STORAGE 到您的清单 - 这是自 API 级别 4 以来的新增内容。

You have to add the permission WRITE_EXTERNAL_STORAGE to your manifest - it is new since API level 4.

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