2013-02-11 104 views
0

我正在實施一個簡單的照片共享客戶端。Android:按鈕上的應用程序崩潰單擊

我在我的用戶界面中有兩個按鈕:一個用於瀏覽圖像,另一個用於發送它。

問題是,當我按下發送按鈕瀏覽並選擇圖像後,應用程序突然崩潰。

下面的代碼:

public class FileTransfer 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_fs); 
     img = (ImageView) findViewById(R.id.ivPic); 
     Button Browse = (Button) findViewById(R.id.bBrowse); 
     Button send = (Button) findViewById(R.id.bSend); 
     final TextView status = (TextView) findViewById(R.id.tvStatus); 

     Browse.setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
         Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); 

        } 
       }); 
     send.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       Socket sock; 
       try { 
        sock = new Socket("MY_PCs_IP", 1149); 
        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(); 
      } 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); 
} 
} 
+3

看起來你正在UI線程上進行網絡操作。不要這樣做。 Google「NetworkOnMainThreadException」 – 323go 2013-02-11 15:18:33

+5

在尋求幫助時發生崩潰時,習慣張貼您的logcat輸出是個好習慣。 – Karakuri 2013-02-11 15:45:24

+0

請發佈logcat的詳細信息。 – Pihu 2013-11-29 10:17:46

回答

0

您正在執行長UI上運行的任務(主)線程。最好考慮使用AsyncTask來執行發送操作。

相關問題