本Blog 接上一篇:【安卓知识点汇总】拍照/访问系统相册小Demo
细节补充:
- 手机如何拍照
- 上传到指定位置
直接上代码:首先定义http网络工具类:HttpPostUtils
这里需要用到:
jar包下载地址:
Httpmime-4.13.jar
当然网络访问,要加权限:
<uses-permission android:name="android.permission.INTERNET"/>
public class HttpPostUtils {
public HttpPostUtils() {
// TODO Auto-generated constructor stub
}
/**
*
* @param path
* @param fileBody
* @param fileName
* @return
*/
public static void sendFormByPost(final String path, final byte[] fileBody,
final String fileName) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
MultipartEntity entity = new MultipartEntity();
entity.addPart("myfile", new ByteArrayBody(fileBody,
fileName));
httpPost.setEntity(entity);
httpClient.execute(httpPost);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}).start();
}
}
接下来就是如何实现目的逻辑了,在主逻辑中:
public class MainActivity extends Activity {
private Button button;
private Button button2;
private ImageView imageView;
private byte[] image_data;// 存储图片的数据
private final String upload_path = "http://192.168.1.1:8080/uploadImage/UploadAction";// 测试路径:上传的路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
imageView = (ImageView) this.findViewById(R.id.imageView1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 拍照的功能
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1001);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpPostUtils.sendFormByPost(upload_path, image_data, "aa.png");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001 && resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, outputStream);
image_data = outputStream.toByteArray();
imageView.setImageBitmap(bitmap);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
当然 布局很简单,同上篇:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="65dp"
android:text="打开照相机拍照" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:text="上传照片" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
好了,补充到这里,如有问题会加以修改。