博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android使用系统相机拍照返回照片,并通过webservice上传到服务器上
阅读量:7102 次
发布时间:2019-06-28

本文共 3587 字,大约阅读时间需要 11 分钟。

hot3.png

如果要在自己的应用中实现拍照的功能,首先要在AndroidManifest.xml文件中添加权限:

启动相机的方法非常简单,通过intent访问MediaStore.ACTION_IMAGE_CAPTURE

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  // 指定拍照 String name = new DateFormat().format("yyyyMMddhhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";imgName = name;mFilePath += name;Uri uri = Uri.fromFile(new File(mFilePath));// 加载路径intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);// 指定存储路径,这样就可以保存原图了startActivityForResult(intent, 1);  // 拍照返回图片

拍照并确认后,Activity的onActivityResult方法会被调用,在这里可以获取图片的数据。

/**     * 拍照返回     */    @Override    public void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        if (resultCode == Activity.RESULT_OK) {            String sdStatus = Environment.getExternalStorageState();            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用                Log.i("TestFile", "SD card is not avaiable/writeable right now.");                return;            }            try {                is = new FileInputStream(mFilePath);// 获取输入流                Bitmap bitmap = BitmapFactory.decodeStream(is);// 把流解析成bitmap                ViewGroup.LayoutParams lp;                lp = img.getLayoutParams();                lp.width = 300;                lp.height = 300 * bitmap.getHeight() / bitmap.getWidth();                img.setLayoutParams(lp);                img.setImageBitmap(bitmap);// 将图片显示在ImageView里            } catch (FileNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } finally {                try {// 关闭流                    is.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }

通过webservice上传图片

/**     * 上传图片     */    private String imgUpload() {        try {            FileInputStream fis = new FileInputStream(mFilePath);            ByteArrayOutputStream baos = new ByteArrayOutputStream();            byte[] buffer = new byte[1024];            int count = 0;            while ((count = fis.read(buffer)) >= 0) {                baos.write(buffer, 0, count);            }            String uploadBuffer = new String(Base64.encode(baos.toByteArray()));  //进行Base64编码            String f = connectWebService(uploadBuffer);            Log.i("connectWebService", "start");            fis.close();            return f;        } catch (Exception e) {            e.printStackTrace();            return "0";        }    }    private String connectWebService(String imageBuffer) {        SoapObject soapObject = new SoapObject(nameSpace, "uploadImage");        soapObject.addProperty("flag", "1");            //标识        soapObject.addProperty("filename", imgName);    // 图片名        soapObject.addProperty("image", imageBuffer);   // 图片字符串        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);        envelope.dotNet = false;        envelope.bodyOut = soapObject;        envelope.setOutputSoapObject(soapObject);        HttpTransportSE httpTranstation = new HttpTransportSE(wsdl);        try {            httpTranstation.call("", envelope);            Object result = envelope.getResponse();            Log.i("connectWebService", result.toString());            return result.toString();        } catch (Exception e) {            e.printStackTrace();            return "0";        }    }

转载于:https://my.oschina.net/yongqingfan/blog/724689

你可能感兴趣的文章
我的友情链接
查看>>
Windows 10:现代化世界中安全与身份的守卫者
查看>>
查找相关数据结构和算法
查看>>
Android系统匿名共享内存Ashmem(Anonymous Shared Memory)驱动程序源代码分析(下)
查看>>
我的友情链接
查看>>
将命名规范的一些列文件合并成一个完整的文件
查看>>
数据恢复过程之:服务器raid5两块硬盘离线数据恢复
查看>>
戴尔Dell Latitude E6410/E6510官方拆机图解维修手册
查看>>
SAS硬盘与SATA硬盘的区别
查看>>
html语义化
查看>>
mysql+mha高可用搭建
查看>>
思绪,飘在青山绿水间
查看>>
不同云服务模式下的安全策略解析
查看>>
mysql删除重复数据只保留一条
查看>>
Cubieboard开发环境与Uboot的SD启动卡制作
查看>>
Linux Shell 脚本限制ssh最大用户登录数
查看>>
我的友情链接
查看>>
VMware虚拟化部署带给我们的便利
查看>>
更新DB2数据表前的小注意
查看>>
我的友情链接
查看>>