简单的 Android 拍照并显示以及获取路径后上传
2014-03-27 00:00
411 查看
简单的 Android 拍照并显示以及获取路径后上传
Activity 中的代码,我只贴出重要的事件部分代码
Layout 布局页面
其中的上传工具类请查看该文章:
http://blog.csdn.net/zhouzme/article/details/18952053
Activity 中的代码,我只贴出重要的事件部分代码
public void doPhoto(View view) { destoryBimap(); String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 1); } else { Toast.makeText(MainActivity.this, "没有SD卡", Toast.LENGTH_LONG).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri uri = data.getData(); if (uri != null) { this.photo = BitmapFactory.decodeFile(uri.getPath()); } if (this.photo == null) { Bundle bundle = data.getExtras(); if (bundle != null) { this.photo = (Bitmap) bundle.get("data"); } else { Toast.makeText(MainActivity.this, "拍照失败", Toast.LENGTH_LONG).show(); return; } } FileOutputStream fileOutputStream = null; try { // 获取 SD 卡根目录 String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos"; // 新建目录 File dir = new File(saveDir); if (! dir.exists()) dir.mkdir(); // 生成文件名 SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS"); String filename = "MT" + (t.format(new Date())) + ".jpg"; // 新建文件 File file = new File(saveDir, filename); // 打开文件输出流 fileOutputStream = new FileOutputStream(file); // 生成图片文件 this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); // 相片的完整路径 this.picPath = file.getPath(); ImageView imageView = (ImageView) findViewById(R.id.showPhoto); imageView.setImageBitmap(this.photo); } catch (Exception e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } } /** * 销毁图片文件 */ private void destoryBimap() { if (photo != null && ! photo.isRecycled()) { photo.recycle(); photo = null; } }
Layout 布局页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/doPhoto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginBottom="10dp" android:text="拍照" android:onClick="doPhoto" /> <TextView android:id="@+id/showContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" /> <ImageView a 3ff0 ndroid:id="@+id/showPhoto" android:layout_width="fill_parent" android:layout_height="250dp" android:scaleType="centerCrop" android:src="@drawable/add" android:layout_marginBottom="10dp" /> </LinearLayout> </ScrollView> </LinearLayout>
其中的上传工具类请查看该文章:
http://blog.csdn.net/zhouzme/article/details/18952053
相关文章推荐
- 简单的 Android 拍照并显示以及获取路径后上传
- 拍照并显示以及获取路径后上传
- Android拍照获取照片路径并上传至服务器+Servlet代码
- Android——获取系统图片/拍照/获取图片路径并显示的Demo
- android 实现调用相机拍照 获取照片路径并上传
- Android应用中拍照后获取照片路径并上传的实例分享
- Android拍照获取照片路径并上传至服务器+Servlet代码
- Android手机调用系统相机拍照、裁剪以及获取Url上传图片
- android SD卡路径问题以及如何获取SDCard 内存
- Android获取内置、外置以及OTG等方式挂载的存储设备路径
- php获取文件夹路径内的图片以及分页显示示例
- Android 4.4 前后版本读取图库图片和拍照完美解决方案-获取文件路径
- Android-->拍照/相册选择照片(从Uri中获取图片路径)
- Android调用系统相册、拍照以及裁剪最简单的实现(兼容7.0)
- android SD卡路径问题以及如何获取SDCard 内存
- android 获取路径目录方法以及判断目录是否存在,创建目录
- android调用照相机拍照获取照片并做简单剪裁
- android6.0通过反射获取USB和U盘以及内部存储路径,区分USB和U盘路径
- Android SD卡路径问题以及如何获取SDCard 内存
- android 获取路径目录方法以及判断目录是否存在,创建目录