[英]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.