如何調用Android的拍照或本地相冊選取,然後再實現相片上傳服務器。
首先是拍照:使用Intent即可,
- final String start = Environment.getExternalStorageState();
- private static final String PHOTOPATH = "/photo/";
- if(start.equals(Environment.MEDIA_MOUNTED)){
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
- if(!file.exists()){
- file.mkdirs();
- }
- tempphontname = System.currentTimeMillis()+".jpg";
- buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
- startActivityForResult(intent, 1);
- }
其次是從本地相冊選:依舊是Intent.
如下代碼:
- if(start.equals(Environment.MEDIA_MOUNTED)){
- Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
- getImage.addCategory(Intent.CATEGORY_OPENABLE);
- getImage.setType("image/jpeg");
- startActivityForResult(getImage, 0);
- }
接下來是主要的:因為調用完系統的方法後,回返回到回調方法onActivityResult(int, int, Intent)中,
在裡面進行主要的照片上傳服務器的操作,
見代碼:
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- ContentResolver resolver = getContentResolver();
- if(requestCode==1)//
- {
- if(resultCode==Activity.RESULT_OK)
- {
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
- {
- ew Thread(new Runnable()
- {
- @Override
- public void run() {
- //進行上傳操作
- }
- }.start();
接下來就可以根據自己的需求搞定了···