最近在做个小项目,想像微博那样可以修改自己的头像,于是乎,需要从本地媒体库中选择头像,显示在imageView中,并且上传到服务器进行保存,以便下次登录自动加载头像。废话不多说,以下是我参考了各种百度和google后结合自己的想法写出来的。
第一步:从媒体库中选择图片,这个代码形式比较固定,其核心的代码是:
/***
* 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的
*/
Intent intent = new Intent();
// 开启Pictures画面Type设定为image
intent.setType("image/*");
// 使用Intent.ACTION_GET_CONTENT这个Action
intent.setAction(Intent.ACTION_GET_CONTENT);
// 取得相片后返回本画面
startActivityForResult(intent, 1);
以上主要是用来打开媒体库,然后选择图片,这些代码我主要将其放到一个按钮的事件监听中。第二步:选择图片后,可以在手机上进行展示,并且获取图片的地址:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==Activity.RESULT_OK) {
/**
* 当选择的图片不为空的话,在获取到图片的途径
*/
Uri uri = data.getData();
Log.e(TAG, "uri = "+ uri);
try {
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, pojo, null, null,null);
if(cursor!=null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//获取图片的路径
String path = cursor.getString(colunm_index);
/***
* 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话
* ,你选择的文件就不一定是图片了,这样的话,我们判断文件的后缀名
* 如果是图片格式的话,那么才可以
*/
if(path.endsWith("jpg")||path.endsWith("png")) {
picPath = path;
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
imageView.setImageBitmap(bitmap);
}else{alert();}
} else {
alert();
}
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void alert() {
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("提示").setMessage("您选择的不是有效的图片")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
picPath = null;
}
})
.create();
dialog.show();
}
以上主要是将其显示在ImageView中,并且获得picPath这个路径。
第三步:将这个图片进行包装成流的形式,利用socket与服务器进行通信,从而向服务器发送图片:
File file = new File(picPath);
if (file != null) {
int length = 0;
//每次上传的大小
byte[] sendBytes = new byte[1024];
Socket socket = null;
OutputStream os = null;
ObjectOutputStream oos = null;
FileInputStream fos = null;
try {
socket= new Socket("192.168.0.106", 5000);
os = socket.getOutputStream();
oos = new ObjectOutputStream(os);
//UPLOAD_IMG是一个常量,主要是服务器来判断是上传操作
oos.writeInt(UPLOAD_IMG);
oos.flush();
fis = new FileInputStream(file);
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
oos.write(sendBytes, 0, length);
oos.flush();
}
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os != null) {
os.close();
}
if(oos != null) {
oos.close();
}
if(fos != null) {
fos.close();
}
if(socket != null) {
socket.close();
}
}
}
以上代码主要是建立与服务器的连接,并且包装成流的形式进行传送,你可以将他放到另外一个按钮的监听事件中。
第四步:接下来是服务器端的代码,主要是接收图片,并且保存到某个位置,核心代码如下:
//上传图片
private void uploadImg() {
byte[] inputByte = new byte[1024];
//user主要是我项目开发时的一个用户对象
//User user = null;
int length = 0;
try {
//user = (User) ois.readObject();
//File file = new File(PIC_PATH + user.getId() + ".jpg");
File file = new File(PIC_PATH + "上传的图片" + ".jpg";
if(!file.exists()) {
file.createNewFile();
}
if(user.getPic() == null) {
jdbc.updateUserPic(user);
}
FileOutputStream fos = new FileOutputStream(file);
while ((length = ois.read(inputByte, 0, inputByte.length)) > 0) {
fos.write(inputByte, 0, length);
fos.flush();
}
fos.close();
oos.writeInt(UPLOAD_SUCCESS);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
当服务器接收了客户端的oos.writeInt(UPLOAD_IMG),这个信息后,判断是上传图片,从而跳转到uploadImg()这个方法中执行,这个方法里面的代码就是上面的。PIC_PATH是PC机上得某一个路劲,大家自定义吧。好了,基本就是这样了,网上其实有很多方法,有利用HTTP进行传输的,说是可以写个Servlet,但是我没试过,也不很熟悉,所以就用我比较熟悉的socket进行传输数据,这个还是比较简单的。如果上面有什么雷同的,请不要见怪(毕竟有参考网上的代码),欢迎指出错误的地方。联系:229047554@qq.com