在 Android 中,要实现一台手机访问另一台手机的资源,你需要使用网络编程的技术。具体来说,你可以使用 Java 的 URL 类和 HttpURLConnection 类来请求和接收资源。
具体实现细节如下:
首先,需要在 AndroidManifest.xml 文件中添加网络权限,否则会抛出网络异常。
<uses-permission android:name="android.permission.INTERNET" />
然后,你可以使用 URL 类来创建一个 URL 对象,并使用 HttpURLConnection 类来请求资源。
URL url = new URL("http://192.168.1.108:54367/sdcard/test.mp4");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
接着,你可以使用 HttpURLConnection 类的 getInputStream() 方法来获取资源的输入流,并使用类似于 FileOutputStream 的方式来保存资源到本地文件。
InputStream inputStream = connection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("/sdcard/ShareFile/test.mp4"));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
请注意,如果手机 A 要访问手机 B 上的资源,那么手机 B 上的资源必须是可访问的,并且手机 B 的防火墙必须打开对应的端口。同时手机A需要获得手机B的IP地址。