Android Wear: how to store image data into watch

末鹿安然 提交于 2019-12-06 07:09:23

问题


I'm looking for how to store image data into my app on Android Wear.

What I want to do are followings:

  1. Take a photo and send it to my watch. (via DataMap)

  2. My watch displays the photo.

  3. When my app on Android Wear restarts, the app displays the photo taken before.

For now, the photo is cleared after restart the app. I want to store the photo.

Are there any ways to save the photo into the watch.

Thanks.

[UPDATE1]

I tried to save an image by using Environment.getExternalStorageDirectory()

But "NOT EXISTS" is returned.

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");
}

[UPDATE2]

I found an error below..

java.lang.IllegalArgumentException: File /storage/emulated/0/test.jpg contains a path separator

[UPDATE3]

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)

[UPDATE4]

I put it. But not change.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

[UPDATE5 SOLVED]

I found that the path "imagePath" was correct. (Sorry. I didn't notice it)

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();
}

回答1:


I believe you are having problems because openFileInput() is for internal storage, not external storage. In fact there is no reason for using Environment.getExternalStorage(). I don't believe the watches have external storage anyway.

Try something like openFileOutput("test.jpg", Context.MODE_WORLD_READABLE); (fyi MODE_WORLD_READABLE is deprecated).

Then use openFileInput("test.jpg") to get it back.

The reason you are getting an error is openFileOutput() cannot have subdirectories.



来源:https://stackoverflow.com/questions/25320747/android-wear-how-to-store-image-data-into-watch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!