Question

I have a simple GDK app that has a menu option on a LiveCard to take a photo. I've followed the instructions in the Documentation to use the FileObserver very similar to the example there. However, once the file is done writing, I attempt to call a method in the binder to my service which then in turn tries to set the image uri on an ImageView in a RemoteView.

In the AndroidManifest.xml I have the following permissions:


<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

MenuActivity:


   @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add_photo:
            startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PIC_REQ);                      
            break;
    }
    return super.onOptionsItemSelected(item);
}

    ...
    private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {

        liveCardService.setPicture(picturePath);
    }
    ...

LiveCardService:


    public class CompPlayerBinder extends Binder {


    public void setPlayerPicture(String picturePath) {
        liveCard.unpublish();

        RemoteViews view = new RemoteViews(getPackageName(), R.layout.photo_layout);

        view.setImageViewUri(R.id.the_photo, Uri.fromFile(new File(picturePath)));
        liveCard.setViews(view);

        Intent i = new Intent(CompPlayerService.this, MenuActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        liveCard.setAction(PendingIntent.getActivity(Service.this, 0, i, 0));
        liveCard.publish(LiveCard.PublishMode.SILENT);




     }
    }

When I run this I'm able to take the photo, but when I tap to accept, I get the following exception in my log

02-13 22:25:53.750 576-3201/? E/CachedFilesManager﹕ Failed to change mode on file

/mnt/sdcard/thumbnail_cache/t_thumb_20140213_222552_748.jpg java.io.IOException: Operation not permitted at com.google.glass.fs.Filesystem.changeMode(Native Method) at com.google.glass.util.CachedFilesManager.save(CachedFilesManager.java:433) at com.google.glass.camera.CameraUtils.saveThumbnailToCachedFiles(CameraUtils.java:76) at com.google.glass.camera.ApiTakePictureActivity$1.doInBackground(ApiTakePictureActivity.java:110) at com.google.glass.camera.ApiTakePictureActivity$1.doInBackground(ApiTakePictureActivity.java:105) at android.os.AsyncTask$2.call(AsyncTask.java:273) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856)

Was it helpful?

Solution 2

Just figured out my problem. I was following the Compass example for the MenuActivity. In that example, after the menu is closed, the activity is finished, because of this onActivityResult never has a chance to run.

@Override
public void onOptionsMenuClosed(Menu menu) {
    super.onOptionsMenuClosed(menu);

    unbindService(mConnection);

    // We must call finish() from this method to ensure that the activity ends either when an
    // item is selected from the menu or when the menu is dismissed by swiping down.
    finish();
}

so what I do instead is only close the menu activity for the other menu options, and leave the activity running until after the photo is done being processed.

OTHER TIPS

I'm Currently using the content resolver in an activity to grab the last picture taken. This is coded for an activity so you may have to adjust for livecard.

There is a dirty project here https://github.com/silenz357/Glass where this is "working". Hope it helps

     String[] projection = new String[]{MediaStore.Images.ImageColumns._ID,MediaStore.Images.ImageColumns.DATA,MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,MediaStore.Images.ImageColumns.DATE_TAKEN,MediaStore.Images.ImageColumns.MIME_TYPE};                  
     final Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");                
    if(cursor != null){                 
         cursor.moveToFirst();
         String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));                    t.setText(path);
         ImageView imageView = (ImageView)findViewById(R.id.imageView1);    
                  image = BitmapFactory.decodeFile(path);                   
         int nh = (int) ( image.getHeight() * (512.0 / image.getWidth()) );                                
         Bitmap scaled = Bitmap.createScaledBitmap(image, 512, nh, true);                       
         imageView.setImageBitmap(scaled);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top