2013-01-13 91 views
0

因此即時通訊嘗試創建一個Android程序,將文件從一個Android手機傳輸到另一個。當發送按鈕被點擊時,我把發送和接收的方法調用,但它似乎並沒有將文件發送到目的地。 (目前有一個手機測試它) 下面是代碼:任何幫助,將不勝感激兩個Android手機之間的Android套接字文件傳輸

import java.io.BufferedInputStream; 
    import java.io.BufferedOutputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.ObjectOutputStream; 
    import java.io.OutputStream; 
    import java.net.ServerSocket; 
    import java.net.Socket; 
    import java.net.UnknownHostException; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.provider.MediaStore; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.TextView; 
    import java.io.DataOutputStream; 


    public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private static final int SELECT_PICTURE = 1; 

    private String selectedImagePath; 
    private ImageView img; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     System.out.println("34"); 
     img = (ImageView) findViewById(R.id.ivPic); 
     System.out.println("36"); 
     ((Button) findViewById(R.id.bBrowse)) 
       .setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
         System.out.println("40"); 
         Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(
           Intent.createChooser(intent, "Select Picture"), 
           SELECT_PICTURE); 
         System.out.println("47"); 
        } 
       }); 
     ; 
     System.out.println("51"); 
     Button send = (Button) findViewById(R.id.bSend); 
     final TextView status = (TextView) findViewById(R.id.tvStatus); 

     send.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Socket sock; 
       try { 
        sock = new Socket("10.0.0.16", 8080); //IP 
        System.out.println("Connecting..."); 

        // sendfile 
          File myFile = new File (selectedImagePath); 
          byte [] mybytearray = new byte [(int)myFile.length()]; 
          FileInputStream fis = new FileInputStream(myFile); 
          BufferedInputStream bis = new BufferedInputStream(fis); 
          bis.read(mybytearray,0,mybytearray.length); 
          OutputStream os = sock.getOutputStream(); 
          System.out.println("Sending..."); 
          os.write(mybytearray,0,mybytearray.length); 
          os.flush(); 

         sock.close(); 

         int filesize=65383; // filesize temporary hardcoded 

         long start = System.currentTimeMillis(); 
         int bytesRead; 
         int current = 0; 

         // create socket 
         ServerSocket servsock = new ServerSocket(8080); 
         while (true) { 
          System.out.println("Waiting..."); 

          Socket sock2 = servsock.accept(); 
          System.out.println("Accepted connection : " + sock2); 

         // receive file 
          byte [] mybytearray2 = new byte [filesize]; 
          InputStream is = sock2.getInputStream(); 
          FileOutputStream fos = new FileOutputStream("/storage/sdcard0/Pictures/Screenshots/IMG-20130112-WA0011.jpeg"); // destination path and name of file 
          //FileOutputStream fos = new FileOutputStream("/storage/sdcard0/Pictures/Screenshots/"); 
          BufferedOutputStream bos = new BufferedOutputStream(fos); 
          bytesRead = is.read(mybytearray2,0,mybytearray2.length); 
          current = bytesRead; 


          do { 
           bytesRead = 
            is.read(mybytearray2, current, (mybytearray2.length-current)); 
           if(bytesRead >= 0) current += bytesRead; 
          } while(bytesRead > -1); 

          bos.write(mybytearray2, 0 , current); 
          bos.flush(); 
          long end = System.currentTimeMillis(); 
          System.out.println(end-start); 
          bos.close(); 



          sock2.close(); 
         } 

       } catch (UnknownHostException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 



      } 
     }); 
    } 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE) { 
       Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       TextView path = (TextView) findViewById(R.id.tvPath); 
       path.setText("Image Path : " + selectedImagePath); 
       img.setImageURI(selectedImageUri); 
      } 
     } 
    } 

    public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    } 


    <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvStatus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <TextView 
     android:id="@+id/tvPath" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Path: " /> 

    <Button 
     android:id="@+id/bBrowse" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Files Available on Your Phone to Send" > 
    </Button> 

    <Button 
     android:id="@+id/bSend" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send Selected File" /> 

    <ImageView 
     android:id="@+id/ivPic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ImageView> 

</LinearLayout> 

權限:

<uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission> 
+0

你是在不同的手機上運行這兩個部分還是同一個?如果它是同一個,它就不會起作用。當偵聽套接字尚不存在時,您正在向套接字發送數據 - 基本Linux操作系統上的connect()調用將失敗。 –

回答

0

我想你的應用程序將被阻止在

sock = new Socket("10.0.0.16", 8080); //IP 
System.out.println("Connecting..."); 

因爲服務器還沒有建立。我對嗎? 「連接...」是否已打印?

相關問題