简体   繁体   English

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

[英]How to Transfer files between two Android phones using Wifi Direct to Download directory of Android phone and not to Package directory

The below line in my code transfers *.apk file to my package directory. 我代码中的以下行将* .apk文件传输到我的包目录中。

 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. 如何进行修改,以便每次将其下载/传输到android手机的下载目录中。 I believe Downloads directory is a default directory in each android phone. 我相信Downloads目录是每个Android手机中的默认目录。

Currently it gets transferred to package directory say for example "com.example.android.wifidirect" 目前,它已转移到软件包目录,例如“ com.example.android.wifidirect”

EDIT I made based on Manish's Solution: 我根据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;
            }
        }

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! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接两部Android手机,通过WIFI在他们之间传输数据 - Connecting two Android phones to transfer data between them over WIFI 使用Wifi在两个Android之间传输数据 - Transfer data between two Android using Wifi 如何通过WiFi直接将一台Android手机连接到多台其他Android手机,并且每台手机都在单独的频道上? - How can I connect one Android phone to multiple other Android phones, using WiFi direct, with each on a separate channel? 列出Android手机下载目录中的文件 - List the files in Download directory of the Android phone 使用wifi直接在Android设备之间传输数据? - Transfer Data between Android devices using wifi direct? 使用USB主机Api在两部Android手机之间传输数据 - Transfer Data between two android phones using Usb host Api 在不使用互联网的情况下,在两个Android手机之间来回传输文件 - transfer file to and fro between two android phones without using the internet 我可以使用 WiFi 在两部未连接路由器的 android 手机之间传输数据/消息吗? - Can I use WiFi to transfer data/messages between two android phones, not connected to a router? 两部Android手机之间的数据传输 - Data transfer between two android phones 如何在两部Android手机之间传输字符串的ArrayList? - How to transfer ArrayLists of Strings between two Android phones?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM