必要使用一個lib不同,我用所謂的jmtpe,我用來讀取該文件,並傳送給主機 代碼:
PortableDeviceManager manager = new PortableDeviceManager();
PortableDevice device = manager.getDevices()[0];
device.open();
System.out.println("Dispositivo: " + device.getModel());
System.out.println("---------------");
System.out.println("Recuperando arquivo do Tablet");
// Iterate over deviceObjects
for (PortableDeviceObject object : device.getRootObjects()) {
// If the object is a storage object
if (object instanceof PortableDeviceStorageObject) {
PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
for (PortableDeviceObject o2 : storage.getChildObjects()) {
if(o2.getOriginalFileName().equals("copagazmobile")){
PortableDeviceFolderObject storage1 = (PortableDeviceFolderObject) o2;
for (PortableDeviceObject o3 : storage1.getChildObjects()) {
if(o3.getOriginalFileName().equals("backupdatabases")){
PortableDeviceFolderObject storage2 = (PortableDeviceFolderObject) o3;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmm");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 1900);
Date data = calendar.getTime();
String idFile = null;
String fileName = null;
for (PortableDeviceObject o4 : storage2.getChildObjects()) {
String dataArq = (o4.getOriginalFileName().substring(15, 28));
try {
if(new Date(format.parse(dataArq).getTime()).after(data)){
data = new Date(format.parse(dataArq).getTime());
idFile = o4.getID();
fileName = o4.getOriginalFileName();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
PortableDeviceToHostImpl32 host = new PortableDeviceToHostImpl32();
try {
File f = new File("c://sqlite//db");
System.out.println("Limpando diretório de manipulação: " + f);
FileUtils.cleanDirectory(f);
System.out.println("Copiando arquivo: " + fileName);
host.copyFromPortableDeviceToHost(idFile, "c:/sqlite/db", device);
f = new File("c://sqlite//db//"+fileName);
File novoArquivo = new File("c://sqlite//db//copagaz_mobile_desenv.db");
InputStream inStream = null;
OutputStream outStream = null;
inStream = new FileInputStream(f);
outStream = new FileOutputStream(novoArquivo);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
f.delete();
} catch (COMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Arquivo mais recente é de: " + data);
}
}
}
}
}
}
manager.getDevices()[0].close();
,併發送回文件:
PortableDeviceManager manager = new PortableDeviceManager();
PortableDevice device = manager.getDevices()[0];
device.open();
System.out.println("---------------");
System.out.println("Enviando arquivo para o Tablet");
// Iterate over deviceObjects
for (PortableDeviceObject object : device.getRootObjects()) {
// If the object is a storage object
if (object instanceof PortableDeviceStorageObject) {
PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
for (PortableDeviceObject o2 : storage.getChildObjects()) {
if(o2.getOriginalFileName().equals("copagazmobile")){
PortableDeviceFolderObject storage1 = (PortableDeviceFolderObject) o2;
for (PortableDeviceObject o3 : storage1.getChildObjects()) {
if(o3.getOriginalFileName().equals("restore")){
PortableDeviceFolderObject host = (PortableDeviceFolderObject) o3;
try {
File f = new File("c://sqlite//db//copagaz_mobile_desenv.db");
System.out.println("Copiando arquivo .db para o Tablet");
host.addAudioObject(f, "0", "1", new BigInteger("12345"));
System.out.println("Arquivo copiado");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
manager.getDevices()[0].close();
究竟是什麼「我試圖使用jmtp但沒有工作」是什麼意思? – f1sh
我讀了storage.getChildObjects(),得到我想要的目錄,但接下來呢? – AgamenonD2
所以你從https://stackoverflow.com/a/23990070/214525複製了一些沒有立即工作的代碼,所以你發佈了這個問題。你有做過什麼研究嗎? – f1sh