3

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();
}
zono
  • 8,366
  • 21
  • 75
  • 113
  • Are you sure that the bitmap is cleared? For me the image is still available after restart with this code: https://github.com/heinrisch/talkclient/blob/master/src/main/java/se/heinrisch/talkclient/TalkClient.java#L187 – Heinrisch Aug 15 '14 at 13:26
  • @Heinrisch, when you use "Wearable.DataApi.putDataItem" to save an object? how long is stored this object and available for "Wearable.DataApi.getDataItems"? It this object persisted when you shutdown the watch or the connection with the mobile device is lost? – culebrins Nov 13 '14 at 11:21
  • I don't really know. I have never experienced that the item is lost. It has worked though shutdowns and connection losses. – Heinrisch Nov 13 '14 at 12:03

1 Answers1

2

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.

Gak2
  • 2,661
  • 1
  • 16
  • 28
  • Thanks a lot. As you said, Environment.getExternalStorage() is available to store images. Greate answer! – zono Aug 15 '14 at 04:29
  • So what is the correct answer? Environment.getExternalStorage() or not? – Lim Thye Chean Jul 28 '15 at 03:23
  • Environment.getExternalStorage() is available. I tested with my app that you can take a look: https://play.google.com/store/apps/details?id=virtualgs.photowatch – Lim Thye Chean Jul 28 '15 at 05:03