简体   繁体   中英

txt file transmission from external storage android. App Crushes

I need to transmit a txt file via WI-FI Direct. The Thing is that app is crushing all the time after begin of transmission(on the client side(that one , that transmits)). There is no problems with Wi-Fi Direct, because I can send Strings etc. with no problems. I believe, that problem is with reading a file. File is on empty SD card. THere is only LOST.DIR there + Alice_commodities.txt (file that I need to transfer).

Here is transmission class

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();                       
    }

}

and class for receiving

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();                       
    }

}

And Manifest file

<?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>

Thank you in advance for help.

UPD Error Log from transmitter

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

You may try commenting the Toast parts in both classes, I had several problems while trying to use them inside an AsyncTask.

protected void onPostExecute(Void result) {           
          super.onPostExecute(result);            
         // Toast toast=Toast.makeText(getApplicationContext(), "File transmited", Toast.LENGTH_SHORT);
         // toast.show();                       
    }

taken from the API of Environment.getExternalStorageDirectory() ?

i'm missing the check, wether the directory is available - if not, it might be null. that may be the case, if you mounted the external storage to your operating system during debugging and may or may not be the error you got before/without debugging. you should unmount the external storage from your operating system, as long as you are debugging.

and if it's feasible, you should use getExternalStoragePublicDirectory for your file transfer, directory as mentioned in the API comments. There's a nice example of how to create a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM