Android截图apk,PC获取手机截图、复制文件、安装APK

PC获取手机截图、复制文件、安装APK

我在eoe上的帖子的链接

PC获取手机截图、复制文件、安装APK

http://www.eoeandroid.com/thread-324986-1-1.html

上次从网上看到了一个利用ddmlib这这个包从手机抓取图片存放到电脑指定目录的一篇帖子,它是个java项目,自己研究了一下,

写了一个demo ,添加了 从电脑中文件复制到设备、将APK文件安装到设备的功能。(ddmlib.jar这个包在SDK目录下tools中lib文件夹下)

package cc.practise;

import java.text.SimpleDateFormat;

public class Main {

/**

* @param args

*/

public static void main(String[] args) {

DeviceManager dm = new DeviceManager(0); //支持多个手机端设备管理,0表示第一个连接的设备

dm.getScreenShot("E://screenShot//", "手机截图_"+toDateTime(System.currentTimeMillis()));

// screenShot.InstallPackage("D://BaiduWenku_Android_10000.apk");

dm.getFileList();//获取手机存储目录列表

dm.pullFile("D:\","/mnt/sdcard/","music.aac");//将电脑中D://msuic.aac文件复制到手机/mnt/sdcard/目录中

dm.InstallPackage("D:\WoChaCha.apk");//将一个apk文件安装到手机中

}

//日期的转换

private static String toDateTime(long time) {

//初始化Formatter的转换格式。

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");

return formatter.format(time);

}

}

[/mw_shl_code]

DeviceManager类

[mw_shl_code=java,true]package cc.practise;

import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.android.ddmlib.AdbCommandRejectedException;

import com.android.ddmlib.AndroidDebugBridge;

import com.android.ddmlib.FileListingService;

import com.android.ddmlib.FileListingService.FileEntry;

import com.android.ddmlib.IDevice;

import com.android.ddmlib.InstallException;

import com.android.ddmlib.RawImage;

import com.android.ddmlib.ShellCommandUnresponsiveException;

import com.android.ddmlib.SyncException;

import com.android.ddmlib.TimeoutException;

public class DeviceManager {

public IDevice device ;

/**

* 构造函数,默认获取第一个设备

*/

public DeviceManager(){

AndroidDebugBridge.init(false);

device = this.getDevice(0);

device.getFileListingService();

System.out.println("设备信息:"+"getAvdName"+device.getAvdName()+"--getName"+device.getAvdName()+"--getSerialNumber"+device.getSerialNumber()+"--getProperty"+device.getProperty("what"));

fileList=device.getFileListingService();

}

/**

* 构造函数,指定设备序号

* @param deviceIndex 设备序号

*/

public DeviceManager(int deviceIndex){

AndroidDebugBridge.init(false); //

device = this.getDevice(deviceIndex);

}

/**

* 直接抓取屏幕数据

*

@return 屏幕数据

*/

public RawImage getScreenShot(){

RawImage rawScreen = null;

if(device!=null){

try {

rawScreen = device.getScreenshot();

} catch (TimeoutException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (AdbCommandRejectedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}else{

System.err.print("没有找到设备");

}

return rawScreen;

}

/**

* 获取图片byte[]数据

* @return 图片byte[]数据

*/

public byte[] getScreenShotByteData(){

RawImage rawScreen = getScreenShot();

if(rawScreen != null){

return rawScreen.data;

}

return null;

}

/**

* 抓取图片并保存到指定路径

* @param path 文件路径

* @param fileName 文件名

*/

public void getScreenShot(String path,String fileName){

System.out.println("设备信息:"+"getAvdName"+device.getAvdName()+"--getName:--"+device.getAvdName()+"--getSerialNumber"+device.getSerialNumber()+"--getProperty"+device.getProperty("what"));

RawImage rawScreen = getScreenShot();

if(rawScreen!=null){

Boolean landscape = false;

int width2 = landscape ? rawScreen.height : rawScreen.width;

int height2 = landscape ? rawScreen.width : rawScreen.height;

BufferedImage image = new BufferedImage(width2, height2,

BufferedImage.TYPE_INT_RGB);

if (image.getHeight() != height2 || image.getWidth() != width2) {

image = new BufferedImage(width2, height2,

BufferedImage.TYPE_INT_RGB);

}

int index = 0;

int indexInc = rawScreen.bpp >> 3;

for (int y = 0; y < rawScreen.height; y++) {

for (int x = 0; x < rawScreen.width; x++, index += indexInc) {

int value = rawScreen.getARGB(index);

if (landscape)

image.setRGB(y, rawScreen.width - x - 1, value);

else

image.setRGB(x, y, value);

}

}

try {

ImageIO.write((RenderedImage) image, "PNG", new File(path + "/" + fileName + ".png"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

* 获取得到device对象

* @param index 设备序号

* @return 指定设备device对象

*/

private IDevice getDevice(int index) {

IDevice device = null;

AndroidDebugBridge bridge = AndroidDebugBridge

.createBridge();// 如果代码有问题请查看API,修改此处的参数值试一下

waitDevicesList(bridge);

IDevice devices[] = bridge.getDevices();

for (int i = 0; i < devices.length; i++) {

System.out.println(devices.toString());

}

if(devices.length < index){

//没有检测到第index个设备

System.err.print("没有检测到第" + index + "个设备");

}

else

{

if (devices.length-1>=index) {

device = devices[index];

}

else

{

device = devices[0];

}

}

return device;

}

/**

* 等待查找device

* @param bridge

*/

private void waitDevicesList(AndroidDebugBridge bridge) {

int count = 0;

while (bridge.hasInitialDeviceList() == false) {

try {

Thread.sleep(500);

count++;

} catch (InterruptedException e) {

}

if (count > 60) {

System.err.print("等待获取设备超时");

break;

}

}

}

public void InstallPackage(String apkFilePath)

{

try {

device.installPackage(apkFilePath, false, "");

} catch (InstallException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private FileListingService fileList;

private FileEntry temp;

/**

* 测试打印出存储目录列表

* @return

*/

public String[] getFileList()

{

fileList=device.getFileListingService();

FileEntry[] arrayFileEntry;

try {

// String str=fileList.getRoot().findChild(fileList.DIRECTORY_SDCARD).getFullPath();

arrayFileEntry=fileList.getChildrenSync(fileList.getRoot());

// System.out.println("print story path:"+fileList.getRoot().findChild(fileList.DIRECTORY_SDCARD).getFullPath());

for (int i = 0; i < arrayFileEntry.length; i++) {

if(arrayFileEntry.getFullPath().equals("/mnt"))

{

System.out.println("我找到mnt目录了!");

FileEntry[] array=fileList.getChildrenSync(arrayFileEntry);

if(array!=null)

for (int j = 0; j < array.length; j++) {

System.out.println("mnt目录下:"+array[j].getFullPath());

}

}

System.out.println("Path:"+arrayFileEntry.getFullPath());

}

} catch (TimeoutException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (AdbCommandRejectedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ShellCommandUnresponsiveException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

/**

* 将一个文件从电脑上复制到手机指定的目录下

* @param localPath:本地的目录

* @param remotePath:手机的目录

* @param fileName:文件名称

*/

public void pullFile(String localPath,String remotePath,String fileName)

{

/* try {

FileEntry[] arrayTemp=fileList.getChildrenSync(temp);

for (int i = 0; i < arrayTemp.length; i++) {

if(arrayTemp.getFullPath().equals(remotePath))

{

System.out.println("I find ttpod path!");

}else

{

System.out.println("没有找到设备中的目录:"+remotePath);

}

System.out.println("arrayTemp:"+arrayTemp.getFullPath());

}*/

try {

device.pushFile(localPath+fileName,remotePath+fileName);

} catch (SyncException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (AdbCommandRejectedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (TimeoutException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/*} catch (SyncException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (AdbCommandRejectedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (TimeoutException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ShellCommandUnresponsiveException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}*/

}

}

ORMLite完全解析(四) 官方文档第四章、在Android中使用

官方文档的第四章原标题是UsingWithAndroid,看过前面的文档友友,看到这里可能会有点晕乎,因为从一开始就在介绍ORMLite在Android中的介绍,但是到第四章

android(6) 扇形菜单实现

一.扇形菜单的实现:借鉴了大神们的源码,那我们来看一下扇形菜单是怎么实现的:效果图:主界面布局:RelativeLayoutxmlns:android=http://schemas.android.com/apk

Android 进行单元测试难在哪-part1

原文链接:AgainstAndroidUnitTests原文作者:MatthewDupree译文出自:开发技术前线www.devtf.cn译者:chaossss校对者:tiiime状态:完成正如我在序中所说,在Android中难于进

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值