android中怎么实现仿手机QQ发表说说添加照片的功能
展开全部
什么叫怎么实现?是界面的实现还是与服务器交互的实现?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先得有root权限,修改系统型号
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
WPS office
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-01-09 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517191
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
这个属于调用系统相册和系统相机进行图片选择的问题
客服端代码:
[java] view plaincopyprint?
package com.cn.lxz.zhaopian;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lxz.utils.android.graphics.GraphicsBitmapUtils;
import lxz.utils.android.net.UploadFile;
import lxz.utils.android.resource.AndroidClass;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Activity_UploadPic extends Activity implements Handler.Callback {
/** Called when the activity is first created. */
ImageView view_pic;
Button view_btn;
EditText view_et;
// 线程通知上传成功
final Handler upLoadhand = new Handler(this);
String[] arrayString = { "拍照", "相册" };
String title = "上传照片";
// 上传的地址
String uploadUrl = "http://192.168.1.101:8080/UploadServlet/UploadServlet?";
String filename = "照片";
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
@Override
public boolean handleMessage(Message msg) {
if (msg.obj != null) {
Drawable drawable = new BitmapDrawable((Bitmap) msg.obj);
view_pic.setBackgroundDrawable(drawable);
Toast.makeText(this, "获得图片并且头像上传成功", 3).show();
}else
{
Toast.makeText(this, "获得图片,但是头像上传失败,请注意配置uploadUrl上传地址", 3).show();
}
return false;
}
// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());
// 对话框
OnClickListener onDialogClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
startCamearPicCut(dialog);// 开启照相
break;
case 1:
startImageCaptrue(dialog);// 开启图库
break;
default:
break;
}
}
private void startCamearPicCut(DialogInterface dialog) {
// TODO Auto-generated method stub
dialog.dismiss();
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2);// 调用前置摄像头
intent.putExtra("autofocus", true);// 自动对焦
intent.putExtra("fullScreen", false);// 全屏
intent.putExtra("showActionIcons", false);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
private void startImageCaptrue(DialogInterface dialog) {
// TODO Auto-generated method stub
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
view_pic = (ImageView) findViewById(R.id.iv);
view_btn = (Button) findViewById(R.id.btn);
view_et = (EditText) findViewById(R.id.et);
view_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder dialog = AndroidClass.getListDialogBuilder(
Activity_UploadPic.this, arrayString, title,
onDialogClick);
dialog.show();
}
});
}
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:
startPhotoZoom(Uri.fromFile(tempFile), 150);
break;
case PHOTO_REQUEST_GALLERY:
if (data != null) {
startPhotoZoom(data.getData(), 150);
}
break;
case PHOTO_REQUEST_CUT:
if (data != null) {
setPicToView(data);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
// 将进行剪裁后的图片显示到UI界面上
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
final Bitmap photo = bundle.getParcelable("data");
new Thread() {
@Override
public void run() {
byte[] photodata = GraphicsBitmapUtils.Bitmap2Bytes(photo);
UploadFile uploadFile = new UploadFile(uploadUrl);
Map parameters = new HashMap();
parameters.put("msg", view_et.getText().toString());
boolean isUploadSuccess = false;
try {
isUploadSuccess = uploadFile.defaultUploadMethod(
photodata, filename, parameters);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (isUploadSuccess) {
upLoadhand.obtainMessage(0, photo).sendToTarget();
} else {
upLoadhand.obtainMessage(-1, null).sendToTarget();
}
}
}.start();
}
}
}
供你参考
客服端代码:
[java] view plaincopyprint?
package com.cn.lxz.zhaopian;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import lxz.utils.android.graphics.GraphicsBitmapUtils;
import lxz.utils.android.net.UploadFile;
import lxz.utils.android.resource.AndroidClass;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Activity_UploadPic extends Activity implements Handler.Callback {
/** Called when the activity is first created. */
ImageView view_pic;
Button view_btn;
EditText view_et;
// 线程通知上传成功
final Handler upLoadhand = new Handler(this);
String[] arrayString = { "拍照", "相册" };
String title = "上传照片";
// 上传的地址
String uploadUrl = "http://192.168.1.101:8080/UploadServlet/UploadServlet?";
String filename = "照片";
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
@Override
public boolean handleMessage(Message msg) {
if (msg.obj != null) {
Drawable drawable = new BitmapDrawable((Bitmap) msg.obj);
view_pic.setBackgroundDrawable(drawable);
Toast.makeText(this, "获得图片并且头像上传成功", 3).show();
}else
{
Toast.makeText(this, "获得图片,但是头像上传失败,请注意配置uploadUrl上传地址", 3).show();
}
return false;
}
// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());
// 对话框
OnClickListener onDialogClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
startCamearPicCut(dialog);// 开启照相
break;
case 1:
startImageCaptrue(dialog);// 开启图库
break;
default:
break;
}
}
private void startCamearPicCut(DialogInterface dialog) {
// TODO Auto-generated method stub
dialog.dismiss();
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("camerasensortype", 2);// 调用前置摄像头
intent.putExtra("autofocus", true);// 自动对焦
intent.putExtra("fullScreen", false);// 全屏
intent.putExtra("showActionIcons", false);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
private void startImageCaptrue(DialogInterface dialog) {
// TODO Auto-generated method stub
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
// TODO Auto-generated method stub
view_pic = (ImageView) findViewById(R.id.iv);
view_btn = (Button) findViewById(R.id.btn);
view_et = (EditText) findViewById(R.id.et);
view_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder dialog = AndroidClass.getListDialogBuilder(
Activity_UploadPic.this, arrayString, title,
onDialogClick);
dialog.show();
}
});
}
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:
startPhotoZoom(Uri.fromFile(tempFile), 150);
break;
case PHOTO_REQUEST_GALLERY:
if (data != null) {
startPhotoZoom(data.getData(), 150);
}
break;
case PHOTO_REQUEST_CUT:
if (data != null) {
setPicToView(data);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
// 将进行剪裁后的图片显示到UI界面上
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
final Bitmap photo = bundle.getParcelable("data");
new Thread() {
@Override
public void run() {
byte[] photodata = GraphicsBitmapUtils.Bitmap2Bytes(photo);
UploadFile uploadFile = new UploadFile(uploadUrl);
Map parameters = new HashMap();
parameters.put("msg", view_et.getText().toString());
boolean isUploadSuccess = false;
try {
isUploadSuccess = uploadFile.defaultUploadMethod(
photodata, filename, parameters);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (isUploadSuccess) {
upLoadhand.obtainMessage(0, photo).sendToTarget();
} else {
upLoadhand.obtainMessage(-1, null).sendToTarget();
}
}
}.start();
}
}
}
供你参考
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询