我正在寻找如何将图像数据存储到我的 Android Wear 应用中。
我想做的是:
拍照并将其发送到我的 watch 。 (通过 DataMap)
我的 watch 显示照片。
当我在 Android Wear 上的应用重新启动时,该应用会显示之前拍摄的照片。
目前,照片在重启应用程序后被清除。我想存储照片。
有什么方法可以把照片保存到 watch 里。
谢谢。
[更新1]
我尝试使用 Environment.getExternalStorageDirectory()
保存图像
但是返回“NOT EXISTS”。
String imagePath = Environment.getExternalStorageDirectory()+"/test.jpg";
try {
FileOutputStream out = openFileOutput(imagePath, Context.MODE_WORLD_READABLE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(bitmapPath);
boolean isExists = file.exists();
if (isExists) {
LOGD(TAG, "EXISTS");
} else {
LOGD(TAG, "NOT EXISTS");
}
[更新2]
我在下面发现了一个错误..
java.lang.IllegalArgumentException: File /storage/emulated/0/test.jpg contains a path separator
[更新3]
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
java.io.FileNotFoundException: /image: open failed: EROFS (Read-only file system)
[更新4]
我说了算。但不会改变。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
[UPDATE5 已解决]
我发现路径“imagePath”是正确的。 (对不起,我没注意到)
String imagePath = Environment.getExternalStorageDirectory() + "/test.jpg";
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
最佳答案
我相信您遇到了问题,因为 openFileInput()
用于内部存储,而不是外部存储。事实上,没有理由使用 Environment.getExternalStorage()
。我不相信 watch 有外部存储。
尝试类似openFileOutput("test.jpg", Context.MODE_WORLD_READABLE);
(仅供引用,MODE_WORLD_READABLE 已弃用)。
然后使用 openFileInput("test.jpg")
将其取回。
出现错误的原因是 openFileOutput() 不能有子目录。
关于安卓磨损 : how to store image data into watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320747/