Android Wear: How to Store Image Data on Your Watch

I am looking for how to store image data in my Android Wear app.

I want to do the following:

  • Take a picture and send it to my watch. (via DataMap)

  • My watch shows a photo.

  • When my Android Wear app reloads, the app displays the photo I took earlier.

The snapshot is now cleared after restarting the application. I want to save a photo.

Is there any way to keep the photo in the watch.

Thank.

[Update1]

I tried to save the image with 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 the 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 myself. But don't change.

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

      

[UPDATE5 SOLVED]

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

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

      

+3


source to share


1 answer


I believe you are in trouble because it is openFileInput()

intended for internal storage and not external storage. It doesn't really make sense to use Environment.getExternalStorage()

. I don't believe the watch has external storage.

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 the error is that openFileOutput () cannot have subdirectories.

+2


source







All Articles