使用Java从SD卡中提取android文件

发布于 2025-01-06 02:47:17 字数 1945 浏览 0 评论 0原文

我想从 android SD 卡(模拟器或设备)中提取文件并将其放在我的 C:\ 驱动器上。

我怎样才能在Java中做到这一点?在命令提示符中,我只需键入:

C:\Users\ME\android-sdks\platform-tools\adb.exe 拉 /sdcard/test_1329402481933.jpg c:\

所以在Java中,我认为它会是这样的:

 Runtime rt = Runtime.getRuntime();
     Process pr = rt.exec("C:\\Users\\ME\\android-sdks\\platform-tools\\adb.exe pull /sdcard/test_1329402481933.jpg c:\\");

     int exitVal = pr.waitFor();
     System.out.println("Exited with error code "+exitVal);

但是,它无法给我这个跟踪:

<块引用>

java.io.IOException:运行 exec() 时出错。命令: [C:\Users\ME\android-sdks\platform-tools\adb.exe,拉, /sdcard/test_1329402481933.jpg,c:]工作目录:空 环境:空于 java.lang.ProcessManager.exec(ProcessManager.java:196) 在 java.lang.Runtime.exec(Runtime.java:225) 在 java.lang.Runtime.exec(Runtime.java:313) 在 java.lang.Runtime.exec(Runtime.java:246) 在 com.blinkbox.client.test.MyTest.takeScreenShot(MyTest.java:138) 在 com.blinkbox.client.test.MyTest.testCanOpenSettings(MyTest.java:66) 在 java.lang.reflect.Method.invokeNative(本机方法) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:205) 在 android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:195) 在 android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:175) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)

在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447) 引起原因:java.io.IOException:权限被拒绝 java.lang.ProcessManager.exec(本机方法) at java.lang.ProcessManager.exec(ProcessManager.java:194) ... 19 更多

我愿意以完全不同的方式来做...只需要完成工作:)

I want to pull a file from the android SD card (emulator or device) and put it on my C:\ drive.

How can I do this in Java? in the command prompt, I simply type:

C:\Users\ME\android-sdks\platform-tools\adb.exe pull
/sdcard/test_1329402481933.jpg c:\

So in Java, I thought it would be something like:

 Runtime rt = Runtime.getRuntime();
     Process pr = rt.exec("C:\\Users\\ME\\android-sdks\\platform-tools\\adb.exe pull /sdcard/test_1329402481933.jpg c:\\");

     int exitVal = pr.waitFor();
     System.out.println("Exited with error code "+exitVal);

but, it fails giving me this trace:

java.io.IOException: Error running exec(). Commands:
[C:\Users\ME\android-sdks\platform-tools\adb.exe, pull,
/sdcard/test_1329402481933.jpg, c:] Working Directory: null
Environment: null at
java.lang.ProcessManager.exec(ProcessManager.java:196) at
java.lang.Runtime.exec(Runtime.java:225) at
java.lang.Runtime.exec(Runtime.java:313) at
java.lang.Runtime.exec(Runtime.java:246) at
com.blinkbox.client.test.MyTest.takeScreenShot(MyTest.java:138) at
com.blinkbox.client.test.MyTest.testCanOpenSettings(MyTest.java:66) at
java.lang.reflect.Method.invokeNative(Native Method) at
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:205)
at
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:195)
at
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:175)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)

at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.io.IOException: Permission denied at
java.lang.ProcessManager.exec(Native Method) at
java.lang.ProcessManager.exec(ProcessManager.java:194) ... 19 more

I am open to do it an entirely different way... Just need to get the job done :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞觅青森が 2025-01-13 02:47:17

答案 1:我认为线索是权限被拒绝

尝试写入不同的目录。

答案 2:尝试“cmd /c ...”

String[] args = {"cmd", "/c", "\"C:\\Users\\ME\android-sdks\\platform-tools\\adb.exe pull /sdcard/test_1329402481933.jpg c:\\test\"",};
Runtime rt = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder(args);
Process pr = pb.start();  

ANSWER 1: I think the clue there is Permission denied

Try writing to a different directory.

ANSWER 2: Try "cmd /c ..."

String[] args = {"cmd", "/c", "\"C:\\Users\\ME\android-sdks\\platform-tools\\adb.exe pull /sdcard/test_1329402481933.jpg c:\\test\"",};
Runtime rt = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder(args);
Process pr = pb.start();  
追风人 2025-01-13 02:47:17
Here is a way to pull a file from an Android. You will need to use ddmlib to find the device.

import com.android.ddmlib.IDevice;
import com.android.ddmlib.SyncService;

    void pullFile(IDevice device, String file, String remotePath, String localPath) {
        SyncService service = getSyncService(device);
        try {
            service.pullFile(
                      remotePath + file
                    , localPath + file
                    , SyncService.getNullProgressMonitor());
        } catch (SyncException | TimeoutException | IOException e) {
            log.error("pull " + file + ":" + e.getMessage());
        }
    }


Here is a way to pull a file from an Android. You will need to use ddmlib to find the device.

import com.android.ddmlib.IDevice;
import com.android.ddmlib.SyncService;

    void pullFile(IDevice device, String file, String remotePath, String localPath) {
        SyncService service = getSyncService(device);
        try {
            service.pullFile(
                      remotePath + file
                    , localPath + file
                    , SyncService.getNullProgressMonitor());
        } catch (SyncException | TimeoutException | IOException e) {
            log.error("pull " + file + ":" + e.getMessage());
        }
    }


戒ㄋ 2025-01-13 02:47:17

从堆栈跟踪 [AndroidTestRunner] 假设您通过 ide 启动它。尝试以管理员身份启动您的IDE,看看是否是这个问题。

From the stacktrace [AndroidTestRunner] asume you start it via ide. Try to start your ide as Administrator to find out if this is the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文