【问题标题】:Android Wear: how to store image data into watchAndroid Wear:如何将图像数据存储到手表中
【发布时间】:2014-08-15 03:11:04
【问题描述】:

我正在寻找如何将图像数据存储到我在 Android Wear 上的应用中。

我想做的是:

  1. 拍照并发送到我的手表。 (通过 DataMap)

  2. 我的手表显示照片。

  3. 当我在 Android Wear 上的应用重新启动时,应用会显示之前拍摄的照片。

目前,重启应用后照片会被清除。我想存储照片。

有什么方法可以将照片保存到手表中。

谢谢。

[更新1]

我尝试使用Environment.getExternalStorageDirectory() 保存图像

但返回“不存在”。

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" />

[已解决更新5]

我发现路径“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();
}

【问题讨论】:

  • 您确定位图已清除?对我来说,使用此代码重新启动后图像仍然可用:github.com/heinrisch/talkclient/blob/master/src/main/java/se/…
  • @Heinrisch 真的!?谢谢!我现在检查一下。
  • @Heinrisch,当您使用“Wearable.DataApi.putDataItem”保存对象时?此对象存储多长时间并可用于“Wearable.DataApi.getDataItems”?当您关闭手表或与移动设备的连接丢失时,此对象是否仍然存在?
  • 我真的不知道。我从未经历过物品丢失。尽管关闭和连接中断,它仍然有效。

标签: android wear-os


【解决方案1】:

我相信您遇到了问题,因为openFileInput() 用于内部存储,而不是外部存储。事实上,没有理由使用Environment.getExternalStorage()。我不相信手表有外部存储。

试试openFileOutput("test.jpg", Context.MODE_WORLD_READABLE); 之类的东西(仅供参考 MODE_WORLD_READABLE 已弃用)。

然后使用openFileInput("test.jpg") 将其取回。

您收到错误的原因是 openFileOutput() 不能有子目录。

【讨论】:

  • 非常感谢。如您所说, Environment.getExternalStorage() 可用于存储图像。很好的答案!
  • 那么正确答案是什么? Environment.getExternalStorage() 与否?
  • Environment.getExternalStorage() 可用。我用我的应用测试过,你可以看看:play.google.com/store/apps/details?id=virtualgs.photowatch
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
相关资源
最近更新 更多