简体   繁体   中英

Android ADB: How to push multiple files, into an emulator?

I'm using ADB in order to copy files from my desktop to a folder on my emulator.

adb push pic.jpg '/storage/emulated/0/DCIM/camera/

This works fine, but I have many files I would like to copy, and I don't want to repeat this command for every file. How can I "push" the contents of a whole directory?

Edit: screenshot of my Android studio:

在此处输入图片说明

For uploading the whole directory, the easiest way is to use Device File Manager in Android Studio.

Open it from the bottom right toolbar and navigate to the directory in the device where you want to upload the data.

设备文件管理器

Right click and click on upload to upload files or directory.

Note: Works only in Android Studio 3.0 and above

要推送当前目录中的所有内容,您可以尝试:

adb push * /storage/emulated/0/DCIM/camera/*

You could use tar to put all your files into a single archive:

tar -cvf all.tar .

Then push that archive to the device:

adb push all.tar  /sdcard 

Finally untar your tar file in the device:

adb shell tar -xvf /sdcard/all.tar -C /sdcard

I couldn't find a solution so I made one:

from ppadb.client import Client as AdbClient
import os
import glob

adb = AdbClient(host='127.0.0.1', port=5037)
devices = adb.devices()    #List of all connected devices


def send_over_adb(device,hostpath,devpath="/storage/emulated/0/"):      # Recursively send folder and files over adb
    if os.path.isfile(hostpath):
        devpath = os.path.join(devpath,hostpath).replace('\\','/') # optimization for windows
        device.push(hostpath, devpath)
    elif os.path.isdir(hostpath):
        for i in glob.glob(hostpath+'\*'):
            print(i)
            send_over_adb(device,i,devpath)
    device.shell('am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard')
    device.shell('am force-stop com.android.gallery3d') #force create thumbnails


hostpath='\path\to\folder\or\file\'
send_over_adb(devices[0],hostpath,devpath)

This function recursively sends over the folders and files while maintaining folder structure and ignores empty directories.

Limitation: file name shouldn't contain forward or back slashes(idk if any os allows that though)

Dependency: pure-python-adb

Tested on: Python3.7.9 on Win 8.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM