Android 1.5 和 Android 1.6 之间海量存储 API 发生了哪些变化
不久前我写了一些代码来将图像保存在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须添加权限
WRITE_EXTERNAL_STORAGE
到您的清单 - 这是自 API 级别 4 以来的新增内容。You have to add the permission
WRITE_EXTERNAL_STORAGE
to your manifest - it is new since API level 4.