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

简介:

简单的 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




目录
相关文章
|
6月前
|
API Android开发 计算机视觉
视觉智能平台有android人脸识别拍照demo?
视觉智能平台有android人脸识别拍照demo么?
65 0
|
数据处理 Android开发
关于安卓glide加载显示进度
安卓glide加载显示进度
338 0
|
8月前
|
存储 数据可视化 Android开发
Android 实现拍照功能,并将图片保存到本地存储
Android 实现拍照功能,并将图片保存到本地存储
437 0
|
存储 XML API
Android调用相机拍照录视频录音以及存储,7.0以上及以下都可使用。
Android调用相机拍照录视频录音以及存储,7.0以上及以下都可使用。
296 0
|
Android开发 C++
Android OpenGL显示任意3D模型文件
Android OpenGL显示任意3D模型文件
Android OpenGL显示任意3D模型文件
|
存储 缓存 Java
Android OpenGL 显示基本图形及相关概念解读
Android OpenGL 显示基本图形及相关概念解读
Android OpenGL 显示基本图形及相关概念解读
|
开发工具 Android开发
Android studio preview不显示
作为一个刚学习Android的小白,装Android studio的过程中就遇到了不少问题,这两天发现preview一直不显示,百度查了一下,很多教程都只是改了一下这里,但是治标不治本啊,新的项目又会出现这个问题。
224 0
Android studio preview不显示
|
XML 缓存 Android开发
Android Studio Dolphin | 2021.3.1不显示布局XML预览
Android Studio Dolphin | 2021.3.1不显示布局XML预览
1967 0
Android Studio Dolphin | 2021.3.1不显示布局XML预览
|
XML 存储 前端开发
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
225 0
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
|
存储 XML 缓存
Android 更换用户头像(拍照、相册选取)
Android 更换用户头像(拍照、相册选取)
432 1
Android 更换用户头像(拍照、相册选取)