如何使用Wifi Direct下载Android手机的目录而不是Package目录在两部Android手机之间传输文件

时间:2013-11-05 05:20:44

标签: android

我的代码中的以下行将* .apk文件传输到我的包目录。

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

如何修改相同,以便每次下载/转移到Android手机的下载目录。我相信Downloads目录是每个Android手机的默认目录。

目前它已转移到包目录中,例如“com.example.android.wifidirect”

我根据Manish的解决方案制作的编辑:

@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;
            }
        }

1 个答案:

答案 0 :(得分:1)

请尝试此代码 -

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

编辑:您也可以尝试使用此代码,希望每次都能创建不同的名称。

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

编辑根据您的具体需求..

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

谢谢!