gpt4 book ai didi

android - 如何使用 Wifi Direct 在两部 Android 手机之间传输文件以下载 Android 手机目录而不是打包目录

转载 作者:行者123 更新时间:2023-11-30 03:17:35 24 4
gpt4 key购买 nike

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

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

如何修改它以便每次下载/传输到安卓手机的下载目录。我相信下载目录是每个安卓手机的默认目录。

目前它被转移到包目录,例如“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;
}
}

最佳答案

请试试这段代码-

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

谢谢!

关于android - 如何使用 Wifi Direct 在两部 Android 手机之间传输文件以下载 Android 手机目录而不是打包目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com