android 调用相机 相册 及图片上传

拍照

01// 启动相机
02    startCamera.setOnClickListener(new OnClickListener() {
03 
04        @Override
05        public void onClick(View v) {
06            // TODO Auto-generated method stub
07 
08            // mCurrentPhotoFile = new File(PHOTO_DIR,getPhotoFileName());
09            // //用当前时间给取得的图片命名
10            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
11            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
12            // Intent intent = new
13            // Intent("android.media.action.IMAGE_CAPTURE");
14            // Uri fromFile = Uri.fromFile(mCurrentPhotoFile);
15            // String string = fromFile.toString();
16            // Log.i("gp", string);
17            // intent.putExtra(MediaStore.EXTRA_OUTPUT,
18            // Uri.fromFile(mCurrentPhotoFile));
19            startActivityForResult(intent, CAPTURE_CODE);
20 
21        }
22    });

调用系统相册

01// 查看相片
02        camera_look_photo.setOnClickListener(new OnClickListener() {
03 
04            @Override
05            public void onClick(View v) {
06                // TODO Auto-generated method stub
07                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
08                intent.setType("image/*");
09                startActivityForResult(intent, IMAGE_CODE);
10 
11            }
12        });

对回调函数进行处理

001protected void onActivityResult(int requestCode, int resultCode, Intent data) {
002    // TODO Auto-generated method stub
003    super.onActivityResult(requestCode, resultCode, data);
004    ContentResolver resolver = getContentResolver();
005   
006    // 拍照
007    if (resultCode != RESULT_OK)
008        return;
009    switch (requestCode) {
010    case CAPTURE_CODE: {
011        // Toast.makeText(Camera.this, "--------------", 2).show();
012       
013        Bitmap set_map = null;
014        paizhao = true;
015        xiangce = false;
016        // doCropPhoto(mCurrentPhotoFile);
017        String sdStatus = Environment.getExternalStorageState();
018        Bundle bundle = data.getExtras();
019        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
020            bitMap = (Bitmap) data.getParcelableExtra("data");
021            if (bitMap == null) {
022                Uri originalUri = data.getData();
023                try {
024                    System.out
025                            .println(Build.MODEL+"----------camera----------------------611-------"
026                                    + originalUri.toString());
027                    byte[] mContent = readStream(resolver
028                            .openInputStream(Uri.parse(originalUri
029                                    .toString())));
030                    if(bitMap!=null)bitMap.recycle();
031                    bitMap = getPicFromBytes(mContent, null);
032                    cam_photo.setImageBitmap(bitMap);
033                   
034                    camera_paht = "/data/sdcard/DCIM/Camera/"+ getPhotoFileName();
035                File aFile = new File("/data/sdcard/DCIM/Camera/"); // 图片上传地址
036                if(!aFile.exists())
037                {
038                    System.out.println("camera-----------------------630------------创建文件---");
039                    aFile.mkdirs();
040                }
041                    ByteArrayOutputStream out = new ByteArrayOutputStream();
042                    if(set_map!=null)set_map.recycle();
043                    set_map = scaleImg(bitMap, x[position_s],
044                            y[position_s]);
045                    set_map.compress(Bitmap.CompressFormat.JPEG, 25, out);
046                    photo = out.toByteArray();
047                    out.close();
048                    try {
049                        FileOutputStream fos = new FileOutputStream(camera_paht); // 文件输出流
050                        try {
051                            fos.write(photo);
052                        //  bitMap.recycle();
053                            fos.close();
054                        } catch (IOException e) {
055                            // TODO Auto-generated catch block
056                            e.printStackTrace();
057                        }
058 
059                    } catch (FileNotFoundException e) {
060                        // TODO Auto-generated catch block
061                        e.printStackTrace();
062                    }
063 
064                } catch (FileNotFoundException e) {
065                    // TODO Auto-generated catch block
066                    e.printStackTrace();
067                } catch (Exception e) {
068                    // TODO Auto-generated catch block
069                    e.printStackTrace();
070                }
071            }
072        } else {
073            //bitMap = (Bitmap) bundle.get("data");
074            Bitmap set_map2 = null;
075            bitMap = (Bitmap) data.getParcelableExtra("data");
076            System.out.println(Build.MODEL+"camera----------------660-------"+bitMap);
077            if (bitMap == null) {
078                Uri originalUri = data.getData();
079                System.out.println("camera----------------------687-------"
080                        + originalUri.toString());
081                byte[] mContent=null;
082                try {
083                    mContent = readStream(resolver.openInputStream(Uri
084                            .parse(originalUri.toString())));
085                    if(bitMap!=null)bitMap.recycle();
086                    bitMap = getPicFromBytes(mContent, null);
087                } catch (Exception e1) {
088                    // TODO Auto-generated catch block
089                    e1.printStackTrace();
090                }
091            }
092           
093            //在这边进行图片的旋转
094            Bitmap resizedBitmap = null;
095            if(Build.MODEL.equals("7260")){
096                Matrix matrix = new Matrix();
097                matrix.postRotate(270);
098                resizedBitmap = Bitmap.createBitmap(bitMap, 0, 0,bitMap.getWidth(), bitMap.getHeight(), matrix, true);
099                bitMap = resizedBitmap;
100            }
101                System.out
102                        .println("camera------------------585----------bitmap---"
103                                + bitMap);
104                // map = scaleImg(bitMap, 250, 420);
105                System.out.println("Camera--------------75------");
106               
107                cam_photo.setImageBitmap(bitMap);
108                ByteArrayOutputStream out = new ByteArrayOutputStream();
109                // 对图片上传时候图片大小的选择
110                if(set_map2!=null)set_map2.recycle();
111                set_map2 = scaleImg(bitMap, x[position_s],
112                        y[position_s]);
113                if(set_map2==null)return;
114                // 将其写入输出流中
115                set_map2.compress(Bitmap.CompressFormat.JPEG, 25, out);
116                photo = out.toByteArray();
117                try {
118                    out.close();
119                } catch (IOException e1) {
120                    // TODO Auto-generated catch block
121                    e1.printStackTrace();
122                }
123                intTobyte(photo.length); // 图片的长度
124                System.out.println("camera-----------------------722-------"+photo_len);
125                camera_paht = PHOTO_DIR + File.separator
126                        + getPhotoFileName();
127                System.out
128                        .println("camera ---------照片存储地址----------419-----"
129                                + camera_paht);
130                File aFile = new File(camera_paht); // 图片上传地址
131                try {
132                    FileOutputStream fos = new FileOutputStream(aFile); // 文件输出流
133                    try {
134                        fos.write(photo);
135                       
136                        fos.close();
137                    } catch (IOException e) {
138                        // TODO Auto-generated catch block
139                        e.printStackTrace();
140                    }
141 
142                } catch (FileNotFoundException e) {
143                    // TODO Auto-generated catch block
144                    e.printStackTrace();
145                }
146            //System.out.println("-----photo----" + photo.length);
147            // Toast.makeText(Camera.this, "" + photo.length, 2).show();
148           
149        }
150        break;
151    }
152        // 相册
153    case IMAGE_CODE: {
154        // camera_paht =PHOTO_DIR+File.separator+getPhotoFileName() ;
155        Bundle bundle = data.getExtras();
156        System.out.println("Camera--------------75------");
157        paizhao = false;
158        xiangce = true;
159        Uri originalUri = data.getData();
160        if (originalUri != null) {
161            Bitmap bitMap = null;
162            try {
163                if(bitMap!=null)bitMap.recycle();
164                bitMap = null;
165                bitMap = MediaStore.Images.Media.getBitmap(resolver,
166                        originalUri);
167                String[] proj = { MediaColumns.DATA };
168                Cursor cursor = managedQuery(originalUri, proj, null, null,
169                        null);
170                // 按我个人理解 这个是获得用户选择的图片的索引值
171                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
172                System.out.println("706-------------camera------"
173                        + column_index);
174                // 将光标移至开头 ,这个很重要,不小心很容易引起越界
175                cursor.moveToFirst();
176 
177                // 最后根据索引值获取图片路径
178                ByteArrayOutputStream out = new ByteArrayOutputStream();
179                uploadPhotPath = cursor.getString(column_index);
180                System.out
181                        .println("camera--------------------561----------- 相册图片的地址--------"
182                                + uploadPhotPath);
183                Bitmap map = scaleImg(bitMap, 250, 420);
184                map.compress(Bitmap.CompressFormat.JPEG, 25, out);
185                photo = out.toByteArray();
186                intTobyte(photo.length); // 图片的长度
187                System.out
188                        .println("camera-----------------565----------相册图片的长度-----"
189                                + photo.length);
190                cam_photo.setImageBitmap(map);             
191            } catch (Exception e) {
192                Toast.makeText(Camera.this, "图片不可用", 3).show();
193            } // 获取相机返回的数据
194 
195        }
196 
197        break;
198    }
199 
200    }
201}

图片的上传,以前是用特定的协议进行上传,不知道是协议的什么地方出现了,一直上传不成功。在网上找到另一种上传方法,Http有自己封装好的方法,

01private void uploadFile2Svr2() {
02        if (paizhao) {
03            file = new File(camera_paht);
04            // paizao图片的地址
05        }
06        if (xiangce) {
07            if(uploadPhotPath==null)   
08                return;
09            file = new File(uploadPhotPath);
10        }
11           
12            System.out.println("camera------------------------807");
13            // 相册图片的地址
14 
15        String httpUrl = path;
16        System.out.println("---------------");
17        HttpPost request = new HttpPost(httpUrl);
18        HttpClient httpClient = new DefaultHttpClient();
19        FileEntity entity = new FileEntity(file, "binary/octet-stream");
20        HttpResponse response;
21        request.setEntity(entity);
22        entity.setContentEncoding("binary/octet-stream");
23        try {
24            response = httpClient.execute(request);
25            System.out.println(response.toString());
26        } catch (ClientProtocolException e) {
27            Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();
28            // TODO Auto-generated catch block
29            e.printStackTrace();
30        } catch (IOException e) {
31            // TODO Auto-generated catch block
32            Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();
33            e.printStackTrace();
34        }
35        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值