我需要通過WI-FI Direct傳輸txt文件。事情是,應用程序開始傳輸後(在客戶端(那一個,傳輸))一直壓榨)。 Wi-Fi Direct沒有問題,因爲我可以毫無問題地發送字符串等。 我相信,這個問題是與閱讀文件。 文件位於空的SD卡上。這裏只有LOST.DIR + Alice_commodities.txt(我需要傳輸的文件)。從外部存儲android的txt文件傳輸。應用程序粉碎
這裏是傳輸類
private class Networking_files_transmit extends AsyncTask<String, Void, Void>
{
public final int port=8888;
@Override
protected Void doInBackground(String... params) {
String ServerIP=params[0];
Socket s=null;
OutputStream out=null;
Context context = null;
BufferedInputStream in=null;
FileInputStream fis=null;
try {
s=new Socket(ServerIP,port);
out= s.getOutputStream();
} catch (UnknownHostException e) {
Log.e("Files transmission", "Can not find host");
e.printStackTrace();
} catch (IOException e) {
Log.e("Files transmission", "Problems with IO");
e.printStackTrace();
}
if(s.isConnected())
{
int count;
byte[] buffer=new byte [1024];
final File myFile=new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Alice_commodities.txt");
try {
in=new BufferedInputStream(new FileInputStream(myFile));
while((count=in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
} catch (FileNotFoundException e) {
Log.e("Files transmission", "Can not find file");
e.printStackTrace();
} catch (IOException e) {
Log.e("Files transmission", "Problems with IO, something wrong with file");
e.printStackTrace();
}
try {
out.close();
in.close();
s.close();
} catch (IOException e) {
Log.e("Files transmission", "Cannot close stream or socket");
e.printStackTrace();
}
}
else
{
Log.e("Files transmission", "Socket could not connect");
try {
out.close();
in.close();
s.close();
} catch (IOException e) {
Log.e("Files transmission", "Cannot close stream or socket");
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast toast=Toast.makeText(getApplicationContext(), "File transmited", Toast.LENGTH_SHORT);
toast.show();
}
}
和類用於接收
private class Networking_input_server_file_receive extends AsyncTask<Void, Void, Void>
{
public final int port=8888;
public long start;
public long end;
@Override
protected Void doInBackground(Void... params) {
ServerSocket ss= null;
Socket incoming=null;
FileOutputStream fos=null;
BufferedOutputStream out=null;
InputStream in=null;
try {
fos = openFileOutput("new.txt", Context.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
Log.e("FileReceiving", "Could not find a file");
e1.printStackTrace();
}
try {
ss=new ServerSocket(port);
ss.setSoTimeout(20000);
incoming= ss.accept();
out = new BufferedOutputStream(fos);
} catch (UnknownHostException e) {
Log.e("FileReceiving", "Could not find a host");
e.printStackTrace();
} catch (IOException e) {
Log.e("FileReceiving", "Problems with IO");
e.printStackTrace();
}
if (incoming.isConnected())
{
start=System.currentTimeMillis();
byte[] buffer = new byte[1024];
int count;
try {
in = incoming.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while((count=in.read(buffer)) > 0){
fos.write(buffer, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
end=System.currentTimeMillis();
fos.close();
incoming.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.e("file receiving", "Connection failed");
try {
fos.close();
incoming.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String s=String.valueOf(end-start);
Toast toast=Toast.makeText(getApplicationContext(), s , Toast.LENGTH_SHORT);
toast.show();
}
}
和清單的文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.wifidirect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.paad.wifidirect.WiFiDirectActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
預先感謝您的幫助。
UPD錯誤從發射機
07-09 10:43:32.653 E/AndroidRuntime(12614): FATAL EXCEPTION: AsyncTask #1
07-09 10:43:32.653 E/AndroidRuntime(12614): java.lang.RuntimeException: An error occured while executing doInBackground()
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.lang.Thread.run(Thread.java:856)
07-09 10:43:32.653 E/AndroidRuntime(12614): Caused by: java.lang.NullPointerException
07-09 10:43:32.653 E/AndroidRuntime(12614): at com.paad.wifidirect.WiFiDirectActivity$Networking_files_transmit.doInBackground(WiFiDirectActivity.java:524)
07-09 10:43:32.653 E/AndroidRuntime(12614): at com.paad.wifidirect.WiFiDirectActivity$Networking_files_transmit.doInBackground(WiFiDirectActivity.java:1)
07-09 10:43:32.653 E/AndroidRuntime(12614): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-09 10:43:32.653 E/AndroidRuntime(12614): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-09 10:43:32.653 E/AndroidRuntime(12614): ... 5 more
沒有錯誤日誌條目提供應用程序崩潰時提供? –
對不起,但我不能在模擬器上執行WiFi Direct應用程序。我在真實設備上運行這個應用程序 –
對不起,如果我現在誤解了你,但由於你應該有一個發送設備和一個接收設備,你可以通過USB調試來調試接收設備(錯誤發生的地方) (通過從eclipse/adt/android studio/intellij啓動應用程序)或通過android調試橋捕獲日誌。無論哪種方式,你應該有權訪問logcat輸出。爲adb方式看到這篇文章:http://stackoverflow.com/questions/2882253/how-do-i-get-the-logfile-from-an-android-device –