1

The below line in my code transfers *.apk file to my package directory.

 final File f = new File(Environment.getExternalStorageDirectory() + "/"
                        + context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
                        + ".apk");

How to modify the same so that each time it gets downloaded/transferred to downloads directory of android phone. I believe Downloads directory is a default directory in each android phone.

Currently it gets transferred to package directory say for example "com.example.android.wifidirect"

EDIT I made based on Manish's Solution:

@Override
        protected String doInBackground(Void... params) {
            try {
                ServerSocket serverSocket = new ServerSocket(8988);
                Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
                Socket client = serverSocket.accept();
                Log.d(WiFiDirectActivity.TAG, "Server: connection done");
                String extStorageDirectory = Environment
                        .getExternalStorageDirectory().toString();
                File folder = new File(extStorageDirectory, "Download");


File file = new File(folder,"wifip2pshared-" + System.currentTimeMillis()+ ".apk");
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }


                Log.d(WiFiDirectActivity.TAG, "server: copying files " + file.toString());
                InputStream inputstream = client.getInputStream();
                copyFile(inputstream, new FileOutputStream(file));
                serverSocket.close();
                return file.getAbsolutePath();
            } catch (IOException e) {
                Log.e(WiFiDirectActivity.TAG, e.getMessage());
                return null;
            }
        }
4

1 回答 1

1

Please try this code-

 File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Download/wifip2pshared-" + System.currentTimeMillis()
                + ".apk");

EDIT: You can try this code also, hope it will help for create different name every time.

String extStorageDirectory = Environment
                    .getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "Download");
            File file = new File(folder, wifip2pshared-" + System.currentTimeMillis()+ ".apk");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

EDIT On your specific demand..

protected String doInBackground(Void... params) {
        try {
            ServerSocket serverSocket = new ServerSocket(8988);
            Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");
            Socket client = serverSocket.accept();
            Log.d(WiFiDirectActivity.TAG, "Server: connection done");
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "Download");
            File file = new File(folder,"wifixyz-" + System.currentTimeMillis()+".apk");
            try {
                file.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            Log.d(WiFiDirectActivity.TAG, "server: copying files " + file.toString());
            InputStream inputstream = client.getInputStream();
            copyFile(inputstream, new FileOutputStream(file));
            serverSocket.close();
            return file.getAbsolutePath();
        } catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
            return null;
        }
    }

Thanks!

于 2013-11-05T05:49:18.137 回答