zono
zono

Reputation: 8584

Android Wear: how to store image data into watch

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

Upvotes: 4

Views: 3948

Answers (1)

Gak2
Gak2

Reputation: 2701

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.

Upvotes: 2

Related Questions