【安卓知识点汇总】 手机拍照上传照片

本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>

好了,补充到这里,如有问题会加以修改。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值