`

android004,简易PC-Android图片发送

阅读更多

 

一、思路:通过PC服务器向Android手机客户机发送图片

1、创建java工程PC服务器

1)、创建服务器对象,带有指定端口

2)、让服务器循环等待客户机的连接

3)、点击按钮出现文件选择器,得到文件路径

4)、取得连结对象上的流对象,再将流传送给客户机

5)、创建一个界面,点击按钮,选择图片,取得图片的信息,发送给客户机

2、Android工程手机客户端

1)、创建好工程

2)、在第一个页面显示之后,创建客户对象(记得加互联网权限android.permission.INTERNET

    得到输入输出流

3)、编辑想要的页面(在第二个页面操作),编辑第一个页面的内容(main.xml

  除第一个Activity外,其他Activity需要在AndroidManifest中注册;

4)、取得各组件的ID并转型为所要的组件类型,跳转页面的条件

5)、点击“接收”按钮,得到服务器传来的流,并转换成bitmap再显示在页面上

 

二、一些细节:

  1、文件选择器JFileChooser不是你想用就能用的,需要借助JFrame才能显示

  2、文件选择,显示提示框,选择文件,并得到其路径的方法:

 

JFileChooser jfc = new JFileChooser();// 创建一个文件选择器
		jfc.showOpenDialog(null);// 提示对话框
		File file = jfc.getSelectedFile();// 得到所选中的文件
		srcFile = file.getAbsolutePath();// 文件的绝对路劲

 3、可能出于使用无线网或其他什么原因,ip可能一下子又变了,所以,创建客户对象的时候一定要小心其IP:

client = new Socket("192.168.1.24", 8090);//创建客户对象(每次都要取得服务器的ip)

4、将所得到的流转换为bitmap,并将其显示到页面上:

 

final Bitmap bitmap = new BitmapFactory().decodeByteArray(data, 0, in);// 转换成bitmap格式的方法,这里由于在下面这个run方法里需要用到bitmap,所以要定义成final类型的
		icon_1.post(new Runnable() {
			public void run() {
				// 将bitmap显示到界面上
				icon_1.setImageBitmap(bitmap);
			}
		});

 5、其他:流的顺序;添加权限;Activity注册,通过找到相应的ID转换成相应的组件也是需要注意的

 

 三、其他拓展:

     EditText中 android:password="true",设置编辑框为密码输入框

     ImageView为图片组件

 

四、代码及注释:

1、PC服务器端:

 

package androidke.flycatdeng.PCServer;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * 图片发送电脑服务器至android手机端
 * @author flycatdeng
 */
public class PhotoSentServer extends JFrame {

	// 声明
	Socket client;// 客户对象
	String srcFile;// 图片地址
	InputStream ins;// 输入流对象
	OutputStream ous;// 输出流对象
	DataOutputStream dos;// 封装成数据输出流对象
	DataInputStream dis;// 封装成数据输入流对象

	// 主函数
	public static void main(String[] args) {
		try {
			PhotoSentServer pss = new PhotoSentServer();
			pss.frameShow();// 显示窗体
			pss.setupServer(8090);// 启动服务器
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 显示窗体的方法
	public void frameShow() {
		this.setTitle("服务器端");// 标题
		this.setSize(200, 200);// 窗体大小
		this.setLayout(new FlowLayout(0, 0, 0));// 流式布局
		JButton jbt = new JButton("发送图片");// 创建一个按钮
		jbt.setActionCommand("sendPhoto");// 按钮监听发送消息
		jbt.addActionListener(al);// 按钮添加事件监听器
		this.add(jbt);// 按钮显示在窗体上
		this.setDefaultCloseOperation(3);// 关闭窗体后退出程序
		this.setVisible(true);// 设置窗体可见
	}

	// 内部类
	ActionListener al = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("sendPhoto")) {
				choosephoto();// 点击按钮后选择照片
				try {
					processPhoto(srcFile);// 发送图片
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	};

	// 选择照片的方法
	public void choosephoto() {
		JFileChooser jfc = new JFileChooser();// 创建一个文件选择器
		jfc.showOpenDialog(null);// 提示对话框
		File file = jfc.getSelectedFile();// 得到所选中的文件
		srcFile = file.getAbsolutePath();// 文件的绝对路劲
		System.out.println("选择的图片成功,地址是:" + srcFile);
	}

	// 发送图片的方法
	public void processPhoto(String srcFile) throws Exception {
		InputStream ins = new FileInputStream(srcFile);// 得到选中文件的流
		DataInputStream dis = new DataInputStream(ins);// 包装
		int fileLen = dis.available();// 得到文件的大小
		System.out.println("图片的大小为" + fileLen);
		if (fileLen > 0) {// 如果有流则执行下面的
			System.out.println("准备读流中");
			dos.writeInt(fileLen);// 将文件的大小以int输出
			byte[] data = new byte[fileLen];
			dis.read(data);// 以字节数组读取
			dos.write(data);// 输出
			dos.flush();// 强制完全输出
			System.out.println("读完流了");
		}
	}

	// 创建服务器的方法
	public void setupServer(int port) throws Exception {
		ServerSocket server = new ServerSocket(port);// 创建服务器对象,带有指定的端口
		System.out.println("创建服务器成功,端口:" + port);
		// 让服务器循环等待客户的连接
		while (true) {
			client = server.accept();// 等待客户连接
			System.out.println("有客户连接进来啦,IP地址:"
					+ client.getRemoteSocketAddress());// 取得打印客户机的IP地址
			ous = client.getOutputStream();// 得到客户输出流
			ins = client.getInputStream();
			dos = new DataOutputStream(ous);
			dis = new DataInputStream(ins);
		}
	}
}

 2、Android客户端:

1)、第一个Activity:

 

package androidke.fycatdeng.lesson004;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FirstActivit extends Activity {

	// 声明
	Button button_send;
	EditText edit_username, edit_password;
	Socket client;
	// 定义静态的输入输出流,以便其他页面使用
	static InputStream ins;
	static OutputStream ous;

	// 重写(这个相当于主函数)
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 显示第一个页面后,创建客户对象
		try {
			client = new Socket("192.168.1.24", 8090);//创建客户对象(每次都要取得服务器的ip)
			System.out.println("客户创建成功!!!");
			ins = client.getInputStream();// 得到客户输入输出流
			ous = client.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
			Toast.makeText(FirstActivit.this, "未连接到服务器,请重新其启动!", 3).show();
		}
		// 可视后取得各个组件的id并强制转换为相应的组件类型
		button_send = (Button) this.findViewById(R.id.button_send);// 提交按钮
		edit_username = (EditText) this.findViewById(R.id.edit_username);// 用户名编辑框
		edit_password = (EditText) this.findViewById(R.id.edit_password);// 密码编辑框
		button_send.setOnClickListener(ocl);// 给按钮添加(设置)监听器
	}

	// 内部匿名类
	OnClickListener ocl = new OnClickListener() {// 这个包引进的时候需要注意,选择的是第二个:android.view.View.OnClickListener;
		// 重写方法
		public void onClick(View v) {
			// 将编辑框的内容得到并转换为字符串
			String username = edit_username.getText().toString();
			String password = edit_password.getText().toString();
			if ("dck".equals(username) && "1".equals(password)) {
				// 用户名个密码都正确后则跳转页面
				Intent intent = new Intent(FirstActivit.this,
						SecondActivity.class);
				// 跳转页面的时候,把那个客户对象也传过去
				// intent.putExtra("clientFlag",client);
				FirstActivit.this.startActivity(intent);
			} else {
				// 如果登录错误则提示
				Toast.makeText(FirstActivit.this, "用户名不存在活密码错误!", 3).show();// 副本,内容,停留时间以秒为单位
			}
		}
	};
}

 2)、第二个Activity:

 

package androidke.fycatdeng.lesson004;

import java.io.DataInputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SecondActivity extends Activity {
	Button button_recivePhoto;
	ImageView icon_1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);// 转到第二个页面second.xml、
		// 取得第二页面的按钮id转型为所需组件类型
		button_recivePhoto = (Button) this
				.findViewById(R.id.button_recivePhoto);
		button_recivePhoto.setOnClickListener(ocl);// 接收按钮绑定监听器
		icon_1 = (ImageView) this.findViewById(R.id.icon_1);
	}

	// 内部匿名类
	OnClickListener ocl = new OnClickListener() {//
		// 这个包引进的时候需要注意,选择的是第二个:android.view.View.OnClickListener;
		// 重写方法
		public void onClick(View v) {
			try {
				System.out.println("点击按钮准备接收图片!");
				recivePhoto();// 点击按钮后接收图片
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	// 接收图片的方法
	public void recivePhoto() throws Exception {
		DataInputStream dis = new DataInputStream(FirstActivit.ins);
		int in = dis.readInt();
		byte[] data = new byte[in];
		dis.read(data);
		System.out.println("读完流了!准备转换图片");
		final Bitmap bitmap = new BitmapFactory().decodeByteArray(data, 0, in);// 转换成bitmap格式的方法,这里由于在下面这个run方法里需要用到bitmap,所以要定义成final类型的
		icon_1.post(new Runnable() {
			public void run() {
				// 将bitmap显示到界面上
				icon_1.setImageBitmap(bitmap);
			}
		});
		System.out.println("转换完了!");
	}
}

 3)、第一个页面:main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <!-- 这下面的前两句是每个组件必要的属性 -->
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="欢迎使用手机CC"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="用户名:"
    />
<EditText
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/edit_username"
/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="密码:"
    />
<EditText
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:password="true"
    android:id="@+id/edit_password"
/>
<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="提交"
    android:id="@+id/button_send"
/>
</LinearLayout>

 4)、第二个页面:second.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <!-- 这下面的前两句是每个组件必要的属性 -->
 <!-- dip表示像素 -->
<Button
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:text="接收"
    android:id="@+id/button_recivePhoto"
/>
<!-- ImageView为图片组件 -->
<ImageView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/icon_1"
    
/>
</LinearLayout>
 

五、效果:


 


  • 大小: 23.1 KB
  • 大小: 9.8 KB
3
0
分享到:
评论

相关推荐

    Android简易版聊天室

    本程序包含3个模块,即Android手机客户端、PC服务器端、PC客户端; 能很好的实现3个窗口的通信。。

    基于Android的传感器app应用.zip

    基于Android的传感器app应用,能够实现传感器功能的测试,采集数据,并将数据可视化。

    简易微信(本机服务器实现)

    该源码为计网课程socket编程实现简易微信的代码实践,内含本机服务器代码(eclipse打开)+PC端简易客户端(eclipse)+android客户端(androidStudio打开)。实现了简单的用户登录、添加好友、socket通信、表情发送等...

    Best Bill Reminder Apps [Android & iOS]-crx插件

    :red_heart:为什么在PC和Mac / Windows上使用“最佳账单提醒”? :red_heart:1. Mobills预算计划程序并跟踪您的财务状况2. Easybills –帐单提醒3. Spendee-预算和费用跟踪程序与计划程序4.带有同步功能的家庭预算...

    Android通过J2ME的录音功能实现简易示波器

    早就有人通过PC声卡的输入(麦克风孔)来做模拟示波器,但是用手机来实现的比较少。用J2ME的MMAPI实现模拟示波器

    Android聊天室

    简易的Android聊天室,PC服务器端用java语言

    基于单片机的简易触摸屏手机设计

    本系统是一个基于单片机的简易触摸屏手机。最近几年,手机的发展日新月异, 特别是 android 操作系统和触摸屏的便捷性,使触摸屏手机迅速普及,触摸屏手机 将是未来手机的主流配置,此系统就是研究触摸屏手机的工作...

    安卓 一个简单新闻系统的服务器和客户端

    服务器使用myeclipse搭建并部署到Tomcat上,客户端在Android Studio上运行

    eztally 简易家庭理财

    用Google Application Engine的简易家庭记账软件,用Android或PC日常记账,软件提供简单的收支分类统计和按月的收支统计。

    Antiyoy:名为“ antiyoy”的android游戏的源代码-Android game source code

    Antiyoy是一个简单的基于回合的android策略。 易于学习,难以掌握。 最新的APK文件和PC版本可在以下位置找到: : 特征: 多人最多可容纳10人的Hotseat多人游戏 随机图生成器 流畅的动画和良好的优化 简易教程 这...

    Easy Movie Texture Video Texture.unitypackage

    简易电影纹理 包含源代码(、iOS) 源路径:EasyMovieTexture/Source 文件夹 iOS(带有 tvOS)源路径:Plugins/iOS 文件夹 [color=var(--color-font-cyan)]此演示 支持的平台:Android、iOS、AppleTV(tvOS) ...

    Android APK+Dex文件反编译及回编译工具(APKDB)v.1.9.2 正式版

    是一款,针对Android OS系统的APK程序,直接反编译修改的工具。 APKDB集合了当今最强悍,最犀利的APK及Dex文件编译工具; 正常安装后,它直接在【鼠标右键】创建快捷菜单; 非常方便汉化工作者,对APK或Dex文件...

    一个简易的java UByte

    java 没有UByte,自己写了一个,主要用于对单字节的转换,通信领域应该会用到,特别是在PC程序转Android程序时

    蘑菇云手机精灵 v4.04.zip

    蘑菇云手机精灵是一款运行于PC端的Android一键刷机工具,能够代替玩家手动刷机的繁琐操作,与相对繁琐的传统刷机方式相比,它能够为广大Android手机玩家在刷机时节省更多的时间和精力。 蘑菇云手机精灵功能特色: 1...

    微信公众号极速开发实战项目源代码

    Android 微信、支付App支付SDK IJPay 让支付触手可及,实现微信、支付宝系列支付 博客 简易的微信公众号管理平台 搭建属于自己的网穿透工具 SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件 项目...

    安卓java读取网页源码-GraduationDesign:毕业设计

    在Android平台中尝试使用QPython运行简易的Python项目 使用Bottle进行简易的Demo编写,熟悉bottle框架的使用 遇到问题 QPython是一个使用Java编写的一个Android项目,虽然可以支持部分的Python库的使用,例如:numpy...

    vue2之简易的pc端短信验证码的问题及处理方法

    &lt;提示语部分不要在意(非重点部分)&gt; 简单说下布局(采用的是 vue的element ui的ui框架 )进行布局操作的 子组件模板部分如下(code部分是很基础的) ... close-on-click-modal=false close-on-press-escape=...

    C++课程设计作业:图书馆管理系统(QT+PC).zip

    Qt支持 Windows、Linux/Unix、Mac OS X、Android、BlackBerry、QNX等多种平台,并为这些不同的平台提供了统一的开发环境。 面向对象 C++是完全面向对象的,这一点和Objective-c等在开发很相似。而Qt又是基于C++一...

    凯福德金业MT4交易平台软件下载

    MetaTrader 4 (凯福德金业MT4交易平台软件)是一款非常优秀的黄金投资看盘交易软件,集行情数据、技术分析和交易操作三大功能于一身,可进行下单、 平仓、限价单、止损、止赢...支持PC电脑版,以及iPhone和Android应用

    Cocos2D-X游戏开发技术精解

    2.2.2 Android开发环境 26 2.2.3 iOS开发环境 35 2.3 引擎中的混合编译 38 2.3.1 Java与C++的混合编译 38 2.3.2 Objective-C与C++的混合编译 41 2.4 引擎的启点 42 2.4.1 应用程序入口 43 2.4.2 引擎应用入口 44 2.5...

Global site tag (gtag.js) - Google Analytics