我想将数据从PC传输到Android手机,我正在使用以下代码,我无法获得工作目录

时间:2021-12-07 16:48:58

I am getting followin error in the code:

我在代码中遇到以下错误:

java.io.IOException: Error running exec(). Command: ["/adb", -s, 9774d56d682e549c, push, "C:\Documents, and, Settings\My, Documents\other\music\b.wma, ", \sdcard\music] Working Directory: null Environment: null

java.io.IOException:运行exec()时出错。命令:[“/ adb”, - s,9774d56d682e549c,push,“C:\ Documents,和Settings \ My,Documents \ other \ music \ b.wma,”,\ sdcard \ music]工作目录:null环境:空值

public class TransferData extends Activity {

private String device_id  ;
private String from = "C:\\Documents and Settings\\My Documents\\other\\music\\b.wma ";
private String to ="\\sdcard\\music";
 private static final String ADB_PUSH = "\"" /*+ Utility.getWorkDir()*/ + File.separator + "adb\" -s %s push \"%s\" %s";
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
     device_id = Secure.getString(getBaseContext().getContentResolver(),Secure.ANDROID_ID); 

     push(device_id,from,to);
}
   /**  Pushes a file to a connected device via ADB
    * @param deviceId Device serial number 
    * @param from Path of source file (on PC)  
    * @param to Path of file destination (on device)  */
   public static void push(String deviceId, String from, String to) {
    try { 




        String cmd = String.format(ADB_PUSH, deviceId, from, to);
        System.out.println("adb push: " + cmd);
        Process p = Runtime.getRuntime().exec(cmd);
        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println("adb: " + line);
            }
        } catch (IOException e) {
            System.out.println(e);
            }
        } 

}

1 个解决方案

#1


0  

You can not access your computer disk drive from android.

您无法从Android访问您的计算机磁盘驱动器。

Just keep the b.wma in the assets folder .

只需将b.wma保留在assets文件夹中即可。

Then use the following code to use b.wma

然后使用以下代码使用b.wma

 try {
    InputStream  is = context.getAssets().open(url);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }

Hope this helps you.

希望这对你有所帮助。

#1


0  

You can not access your computer disk drive from android.

您无法从Android访问您的计算机磁盘驱动器。

Just keep the b.wma in the assets folder .

只需将b.wma保留在assets文件夹中即可。

Then use the following code to use b.wma

然后使用以下代码使用b.wma

 try {
    InputStream  is = context.getAssets().open(url);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
      }

Hope this helps you.

希望这对你有所帮助。