首页 \ 问答 \ 桌面java应用程序通过USB复制和传输android数据(Desktop java app copy and transfer android data via USB)

桌面java应用程序通过USB复制和传输android数据(Desktop java app copy and transfer android data via USB)

我有一个桌面java应用程序,也是一个Android应用程序。 两个应用程序一起工作

桌面应用程序中的用户有一个按钮来启动设备数据应用程序与计算机应用程序之间的传输,反之亦然。

所以我需要用简单的USB线传输数据,没有互联网连接/ WiFi /蓝牙/ adb。

我发现两个可以在Windows上运行的Java MTP库来解决我的问题,以及android的USB Host / accesory功能:

  • jMTP成功识别我的Android设备,文件夹和其他内容

    我成功地在计算机--->设备中传输文件,但是当我尝试在设备中传输文件时出错 - >计算机

    我在解释后把我的代码放了。

  • jusbpmp但我没有可能转移设备--->电脑。

  • USB主机/附件没有用,因为传输是从桌面应用程序启动的,当我在android开发者指南网站上阅读时,它似乎与我需要的不一致,或者可能是用户从设备开始传输。

我尝试从1周到成功完成这项任务,但似乎我需要帮助。

Java + jMTP代码

private static void jMTPeMethode() 
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            PortableDevice device = manager.getDevices()[0];
            // Connect to USB tablet
            device.open();

            System.out.println(device.getModel());

            System.out.println("---------------");

            // 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().equalsIgnoreCase("Test"))
                        {

                            //Device to computer not working
                            PortableDeviceToHostImpl32 copy = new PortableDeviceToHostImpl32();
                            try 
                            {
                                copy.copyFromPortableDeviceToHost(o2.getID(), "C:\\TransferTest", device);
                            } catch (COMException ex) 
                            {

                            }

    //                      //Host to Device working
    //                      BigInteger bigInteger1 = new BigInteger("123456789");
    //                      File file = new File("c:/GettingJMTP.pdf");
    //                      try {
    //                          storage.addAudioObject(file, "jj", "jj", bigInteger1);
    //                      } catch (Exception e) {
    //                          //System.out.println("Exception e = " + e);
    //                      }
                        }
                        System.out.println(o2.getOriginalFileName());
                    }
                }
            }

            manager.getDevices()[0].close();
        }

这是代码和错误的结果

`

    Nexus 9
    ---------------
    Music
    Podcasts
    Ringtones
    Alarms
    Notifications
    Pictures
    Movies
    Download
    DCIM
    Android
    ! Failed to get IStream (representing object data on the device) from IPortableDeviceResources, hr = 0x80070057
    test
    ReleveData

`

我在互联网上读到0x80070057是一个通用的Windows异常。

编辑:

Windows站点说 hr错误ERROR_INVALID_PARAMETER 0x80070057:应用程序提供的参数无效。

但我没有看到女巫参数无效


这里是用于传输数据设备到计算机的库的C类链接 ,你可以看到我的错误行230。

这是我使用的jMTP库。

你能帮助我,还是用其他方式去做我需要的东西(Usb4Java,libUSB)? 我将非常感激。

谢谢你提前。


I have a desktop java app, and also an android app. Two app work together.

The user in desktop app have a button to launch the transfer between device data app to computer app and vice versa.

So I need to transfer data with a simple USB cable, and without internet connection/WiFi/Bluetooth/adb.

I found two Java MTP library that works on Windows to resolve my problem, and the USB Host/accesory fonctionnality of android:

  • jMTP successfully recognizes my Android devices, folder, and other things

    I have success to transfer a file in computer ---> device, but i have an error when i try to transfer a file in device ---> computer

    I put my code after the explaination.

  • jusbpmp but i don't have the possibility to transfer device ---> computer.

  • USB Host/accesory not usefull because transfer are launch from desktop app, and when i read on the android developper guide website, it seems to be not correspond from what i need, or maybe if the user start transfer from the device.

I try from 1 week to success in this task but it seems i need help.

Java + jMTP code

private static void jMTPeMethode() 
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            PortableDevice device = manager.getDevices()[0];
            // Connect to USB tablet
            device.open();

            System.out.println(device.getModel());

            System.out.println("---------------");

            // 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().equalsIgnoreCase("Test"))
                        {

                            //Device to computer not working
                            PortableDeviceToHostImpl32 copy = new PortableDeviceToHostImpl32();
                            try 
                            {
                                copy.copyFromPortableDeviceToHost(o2.getID(), "C:\\TransferTest", device);
                            } catch (COMException ex) 
                            {

                            }

    //                      //Host to Device working
    //                      BigInteger bigInteger1 = new BigInteger("123456789");
    //                      File file = new File("c:/GettingJMTP.pdf");
    //                      try {
    //                          storage.addAudioObject(file, "jj", "jj", bigInteger1);
    //                      } catch (Exception e) {
    //                          //System.out.println("Exception e = " + e);
    //                      }
                        }
                        System.out.println(o2.getOriginalFileName());
                    }
                }
            }

            manager.getDevices()[0].close();
        }

This is the result of the code and the error

`

    Nexus 9
    ---------------
    Music
    Podcasts
    Ringtones
    Alarms
    Notifications
    Pictures
    Movies
    Download
    DCIM
    Android
    ! Failed to get IStream (representing object data on the device) from IPortableDeviceResources, hr = 0x80070057
    test
    ReleveData

`

I read in internet 0x80070057 is a generic windows exception .

Edit:

Windows site say for the hr error ERROR_INVALID_PARAMETER 0x80070057 : The parameter supplied by the application is not valid.

But i don't see witch parameter is not valid


Here is the link of C class of the library use for transfer data device to computer, you can see my error line 230.

And this is the jMTP library i use.

Can you help me, or purpose an other way to do what i need(Usb4Java, libUSB) ? I shall be really grateful.

Thanks by advance.


原文:https://stackoverflow.com/questions/29645275
更新时间:2023-02-17 07:02

最满意答案

  • 如果希望以巴黎为center初始化地图,请将其用于center MapOption (而不是标记的中心属性)。
function initialize() {
    var mapDiv = document.getElementById('map-canvas');
    var mapOptions = {
        center: paris,
        zoom: 8,
        zoomControl: false,
        scrollwheel: false,
        disableDoubleClickZoom: true
    }
    map = new google.maps.Map(mapDiv, mapOptions);
  • infowindow“auto pan”正在干扰地图中心(你正在打开3个infowindows,一个接一个,平底锅互相干扰。将{disableAutoPan: true}添加到infowindow选项。

工作小提琴

工作代码片段:

var map;
var paris = new google.maps.LatLng(48.8566667, 2.3509871);
var tunis = new google.maps.LatLng(36.845235, 10.164723);
var hongkong = new google.maps.LatLng(22.298812, 114.172175);



function Tunisie(controlDiv2, map) {

  controlDiv2.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv2.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(tunis);
  });

}

function HongKong(controlDiv3, map) {
  controlDiv3.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv3.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(hongkong);
  });

}

function Paris(controlDiv, map) {

  controlDiv.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);
  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(paris);
  });

}

function initialize() {
  var mapDiv = document.getElementById('map-canvas');
  var mapOptions = {
    center: paris,
    zoom: 8,
    zoomControl: false,
    scrollwheel: false,
    disableDoubleClickZoom: true
  }
  map = new google.maps.Map(mapDiv, mapOptions);


  var tunisieControlDiv = document.createElement('div');
  var tunisieControl = new Tunisie(tunisieControlDiv, map);

  tunisieControlDiv.index = 3;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(tunisieControlDiv);

  var hongkongControlDiv = document.createElement('div');
  var hongkongControl = new HongKong(hongkongControlDiv, map);

  hongkongControlDiv.index = 2;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(hongkongControlDiv);

  var parisControlDiv = document.createElement('div');
  var parisControl = new Paris(parisControlDiv, map);

  parisControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(parisControlDiv);

  var marker2 = new google.maps.Marker({
    position: tunis,
    map: map,
    title: 'Tunis'
  });

  var marker3 = new google.maps.Marker({
    position: hongkong,
    map: map,
    title: 'Hong Kong'
  });

  var marker = new google.maps.Marker({
    position: paris,
    map: map,
    title: 'Paris'
  });

  var infowindow2 = new google.maps.InfoWindow({
    content: "Tunis",
    disableAutoPan: true

  });
  var infowindow3 = new google.maps.InfoWindow({
    content: "Hong Kong",
    disableAutoPan: true

  });
  var infowindow = new google.maps.InfoWindow({
    content: "Paris",
    disableAutoPan: true
  });

  google.maps.event.addListener(marker2, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker2.getPosition());
  });
  google.maps.event.addListener(marker3, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker3.getPosition());
  });
  google.maps.event.addListener(marker, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker.getPosition());
  });

  infowindow2.open(map, marker2);
  infowindow3.open(map, marker3);
  infowindow.open(map, marker);

}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>


  • If you want the map to be initialized centered on Paris, use that for the center MapOption (not the center property of a marker).
function initialize() {
    var mapDiv = document.getElementById('map-canvas');
    var mapOptions = {
        center: paris,
        zoom: 8,
        zoomControl: false,
        scrollwheel: false,
        disableDoubleClickZoom: true
    }
    map = new google.maps.Map(mapDiv, mapOptions);
  • the infowindow "auto pan" is interferring with the map center (you are opening 3 infowindows, one right after another, the pans are interfering with each other. Add {disableAutoPan: true} to the infowindow options.

working fiddle

working code snippet:

var map;
var paris = new google.maps.LatLng(48.8566667, 2.3509871);
var tunis = new google.maps.LatLng(36.845235, 10.164723);
var hongkong = new google.maps.LatLng(22.298812, 114.172175);



function Tunisie(controlDiv2, map) {

  controlDiv2.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv2.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(tunis);
  });

}

function HongKong(controlDiv3, map) {
  controlDiv3.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv3.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);

  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(hongkong);
  });

}

function Paris(controlDiv, map) {

  controlDiv.style.padding = '5px';

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = '';
  controlDiv.appendChild(controlUI);

  var controlText = document.createElement('div');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b></b>';
  controlUI.appendChild(controlText);
  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(paris);
  });

}

function initialize() {
  var mapDiv = document.getElementById('map-canvas');
  var mapOptions = {
    center: paris,
    zoom: 8,
    zoomControl: false,
    scrollwheel: false,
    disableDoubleClickZoom: true
  }
  map = new google.maps.Map(mapDiv, mapOptions);


  var tunisieControlDiv = document.createElement('div');
  var tunisieControl = new Tunisie(tunisieControlDiv, map);

  tunisieControlDiv.index = 3;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(tunisieControlDiv);

  var hongkongControlDiv = document.createElement('div');
  var hongkongControl = new HongKong(hongkongControlDiv, map);

  hongkongControlDiv.index = 2;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(hongkongControlDiv);

  var parisControlDiv = document.createElement('div');
  var parisControl = new Paris(parisControlDiv, map);

  parisControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(parisControlDiv);

  var marker2 = new google.maps.Marker({
    position: tunis,
    map: map,
    title: 'Tunis'
  });

  var marker3 = new google.maps.Marker({
    position: hongkong,
    map: map,
    title: 'Hong Kong'
  });

  var marker = new google.maps.Marker({
    position: paris,
    map: map,
    title: 'Paris'
  });

  var infowindow2 = new google.maps.InfoWindow({
    content: "Tunis",
    disableAutoPan: true

  });
  var infowindow3 = new google.maps.InfoWindow({
    content: "Hong Kong",
    disableAutoPan: true

  });
  var infowindow = new google.maps.InfoWindow({
    content: "Paris",
    disableAutoPan: true
  });

  google.maps.event.addListener(marker2, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker2.getPosition());
  });
  google.maps.event.addListener(marker3, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker3.getPosition());
  });
  google.maps.event.addListener(marker, 'click', function() {
    map.setZoom(8);
    map.setCenter(marker.getPosition());
  });

  infowindow2.open(map, marker2);
  infowindow3.open(map, marker3);
  infowindow.open(map, marker);

}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

相关问答

更多
  • 如果希望以巴黎为center初始化地图,请将其用于center MapOption (而不是标记的中心属性)。 function initialize() { var mapDiv = document.getElementById('map-canvas'); var mapOptions = { center: paris, zoom: 8, zoomControl: false, scrollwheel: false, ...
  • 您已经在为GMSMapView创建IBOutlet 它将为您创建一个地图实例。 因此,无需再创建一个GMSMapView实例并将其分配给类变量。 override func viewDidAppear(_ animated: Bool) { // Create a GMSCameraPosition that tells the map to display the // coordinate -33.86,151.20 at zoom level 6. let camera ...
  • 我不知道你怎么能拥有一个功能齐全的地图,内容最重要。 要使内容可见,它必须具有更高的z-index,这将反过来掩盖地图中的元素。 您可以将'map-canvas'设置为正文,然后将div放置在地图周围的区域内,但远离POI或地图控件工具,并使用RGBA颜色作为内容div的背景颜色(可能50%的不透明度),但由于顶部的div,因此访问者无法访问该内容。 这样的事情应该让你开始......: Geolocation</titl ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/dyggdtjqueryzqxs_353" target="_blank">带有谷歌地图的Jquery无法正确显示(Jquery with google maps is not showing correctly)</a><i>[2021-07-16] </i></h2> </div> <div class="tw_li_cont"> 延迟地图的初始化,直到第一次显示带有地图的选项卡。 <script type="text/javascript"> $(function() { var map = null; $("#tabs").on("tabsactivate", function (event, ui) { if ((ui.newPanel.attr('id') == 'two') && !map) { var yourStartLatLng = new google.map ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/ggdtsharpmapdtbjwzqdq_354" target="_blank">谷歌地图作为我的sharpmap地图的背景未正确对齐(Google map as background to my sharpmap map not aligning correctly)</a><i>[2022-03-11] </i></h2> </div> <div class="tw_li_cont"> 我发现谷歌地图图层没有居中的问题。 原因是我使用QGIS将我的ESRI shapefile从WGS84(EPSG:4326)格式转换为Spherical Mercator(EPSG:900913)并更改了坐标格式但是 Google地图使用Google Maps Global Mercator(球形墨卡托)。 当我使用在线转换器来测试这个,你可以在这里找到。 当我给出结果坐标时,它很好。 现在,我所要做的就是找到一种转换Google Maps Global Mercator(球形墨卡托)的方法。 谢谢你的帮助 ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/gugeditubujia_359" target="_blank">谷歌地图不加载(google map is not loading)</a><i>[2022-10-08] </i></h2> </div> <div class="tw_li_cont"> 问题:您必须在此处添加Google地图API密钥才能访问Google地图。 解决方案::这是与我的api键一起工作,请自己创建并替换为我的。 请参阅说明: 在这里获取您的api密钥! <DOCTYPE html> <head> <title>