简单的 Android 拍照并显示以及获取路径后上传

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                android: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




更多相关文章

  1. android tips:从资源文件中读取文件流显示
  2. android NDK/JNI-实例开发流程
  3. android recovery 模式启动进入流程
  4. Android--sharepreference总结
  5. Android(安卓)sdcard文件读写操作
  6. 解决Android模拟器打不开的问题!...
  7. Android(安卓)tips2
  8. Pycharm安装PyQt5的详细教程
  9. NPM 和webpack 的基础使用

随机推荐

  1. 如何在不同Android设备中得到有效的Audio
  2. Android中程序的停止状态详细介绍
  3. Ubuntu上搭建android BSP的开发编译环境
  4. android 7.0新特性,新功能,GMS4.0新特性
  5. Airbnb 动画库Lottie
  6. Android多线程处理机制中的Handler使用介
  7. 【Android】Macbook Pro 10.14(macOS Moja
  8. Android(安卓)Fragmnet的使用新体会
  9. Android中Activity启动模式学习笔记
  10. 关于Android Studio构建失败,导入新项目打