2

我想要做的事:Android的工作室拍照後發送串過插座[谷歌眼鏡]

用戶說「找不到我的鑰匙」 谷歌眼鏡拍照並存儲的字符串什麼你說在一個端口上發送一個套接字。 創建了第二個插槽來發送圖片,因此接收器會有聲音字符串和圖片以便進一步處理。

問題:在拍攝照片

後,我有一些Toast.makeText信息與插座的創建,正在因爲某些原因忽略了一起。

所有套接字創建的下onActivityResult 語音檢測情況發生在onCreate

代碼如下:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.FileObserver; 
import android.provider.MediaStore; 
import android.speech.RecognizerIntent; 
import android.util.Log; 
import android.widget.Toast; 
import com.google.android.glass.content.Intents; 
import java.io.File; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

public class CameraActivity extends Activity { 

    private CameraSurfaceView cameraView; 


    private static final int TAKE_PICTURE_REQUEST = 1; 

    //Take the picture only if the string take_picture from voice control allows for it. 
    private void takePicture() { 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, TAKE_PICTURE_REQUEST); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(resultCode != RESULT_CANCELED) 
     { 


      if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) { 
       String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH); 
       String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); 

       processPictureWhenReady(picturePath); 
       Toast.makeText(getApplicationContext(), "Proceeding to Socket creation.", Toast.LENGTH_LONG).show(); 

       //Create the java socket and send the file to the android device 
       //Need to send out the voice command to the Android phone and the picture 
       //for now, send the voice data 

       try { 

        Toast.makeText(getApplicationContext(), "Socket created on port 40404", Toast.LENGTH_LONG).show(); 

        try { 
         //Define the socket: 
         //Firewall problem. 
         Socket skt = new Socket(InetAddress.getLocalHost(), 40404); 
         //Send the voice data 
         OutputStream outstream = skt.getOutputStream(); 
         PrintWriter out = new PrintWriter(outstream); 
         Toast.makeText(getApplicationContext(), "Sending data to Android Device", Toast.LENGTH_LONG).show(); 
         out.print(voiceglobal); 

         //The commented out code below will eventually send the picture that you take with the device over a socket 
         //to a recieving device.You can ignore this for now. 


         //long length = pictureToSend.length(); 

         //Create a new socket for sending the picture. 
          /* 
          Socket pic_socket = new Socket(InetAddress.getLocalHost(), 50505); 
          byte bytes[]; 
          ObjectInputStream ois = new ObjectInputStream(pic_socket.getInputStream()); 
          FileOutputStream fos = null; 
          try { 
           bytes = (byte[])ois.readObject(); 
           fos = new FileOutputStream(pictureToSend); 
           fos.write(bytes); 
          } catch (ClassNotFoundException e) 
          { 
           e.printStackTrace(); 

          } finally { 
           if(fos!= null) 
           { 
            fos.close(); 
           } 
          } 
          */ 
         ////////////////////////////////////////////// 
         ////////////////// 


        } catch (UnknownHostException e) { 

         //Handle this. 
         System.err.print(e); 
        } 
       } catch (IOException v) { 
        // handle this. 
        System.err.print(v); 
        } 
      } 

      //Define the socket: 

      //Make sure this works and then write the code for 


      ///////////////////////////////////////////////////////////////////////// 
      ////////////////////////////////////////////////////////////////////// 


     } //if resultCode != RESULT_CANCELED 

     super.onActivityResult(requestCode, resultCode, data); 
    } 

    //Define global path to picture file. 
    //Test the path by using the recieving connection. 
    File pictureToSend; 


    private void processPictureWhenReady(final String picturePath) { 
     final File pictureFile = new File(picturePath); 
     //////////////////////////////////////////////////////// 
     ////// 
     pictureToSend = pictureFile; //Global for sending the picture. 
     //////////////////////////////////////////////////////// 
     ///// 

     if (pictureFile.exists()) { 
      // The picture is ready; process it. 

     } else { 
      // The file does not exist yet. Before starting the file observer, you 
      // can update your UI to let the user know that the application is 
      // waiting for the picture (for example, by displaying the thumbnail 
      // image and a progress indicator). 

      final File parentDirectory = pictureFile.getParentFile(); 
      FileObserver observer = new FileObserver(parentDirectory.getPath(), 
        FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) { 
       // Protect against additional pending events after CLOSE_WRITE 
       // or MOVED_TO is handled. 
       private boolean isFileWritten; 

       @Override 
       public void onEvent(int event, String path) { 
        if (!isFileWritten) { 
         // For safety, make sure that the file that was created in 
         // the directory is actually the one that we're expecting. 
         File affectedFile = new File(parentDirectory, path); 
         isFileWritten = affectedFile.equals(pictureFile); 

         if (isFileWritten) { 
          stopWatching(); 

          // Now that the file is ready, recursively call 
          // processPictureWhenReady again (on the UI thread). 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            processPictureWhenReady(picturePath); 
           } 
          }); 
         } 
        } 
       } 
      }; 
      observer.startWatching(); 
     } 
    } 


    public byte[] imgbyte; 
    public String voiceglobal; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //Initiate CameraView 
     cameraView = new CameraSurfaceView(this); //Calls CameraSurfaceView 

     // Set the view 
     this.setContentView(cameraView); 



     //VOICE DETECTION HAPPENS HERE. 
     ArrayList<String> voiceRes = getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS); 
     String Voicedata = voiceRes.toString(); 
     Log.d("Voicedata was ", Voicedata); 
     voiceglobal = Voicedata; 


     // "Dynamic" voice detection ... 
     if((Voicedata.equals("[take a picture]")) || (Voicedata.equals("[find my cup]")) || (Voicedata.equals("[find my keys]"))) 
     { 
      //Then take the picture. 
      takePicture(); 

     } // if voicedata equals 

     else if(!Voicedata.equals("take a picture")) 
     { 
      //Make a toast that says it's wrong. 
      Toast.makeText(getApplicationContext(), "Try again", Toast.LENGTH_LONG).show(); 
     } 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Do not hold the camera during onResume 
     if (cameraView != null) { 
      cameraView.releaseCamera(); 
     } 


    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     // Do not hold the camera during onPause 
     if (cameraView != null) { 
      cameraView.releaseCamera(); 
     } 
    } 






} 

在此先感謝非常多。一旦它爲用戶提供幫助,我會發布整個項目。

+0

'onActivityResult()'據推測稱UI線程,你在哪裏不允許進行任何網絡操作的,但是很可能遠離你唯一的問題,而且可能甚至不是你的,如果你是第一個問題沒有得到相關的例外。 –

+0

謝謝 - 我創建了一個新線程,以自己的方法運行我的所有套接字創建工具,並且當我回到Google眼鏡設備時,它肯定會看到它是否有效。 – user2852171

回答

0
  1. 檢查,如果您有Internet的權限在您的清單 <uses-permission android:name="android.permission.INTERNET" />

  2. 移動所有的網絡代碼從主UI線程。否則,您將在日誌中獲得android.os.NetworkOnMainThreadException。更多信息here