照相机的应用

Activity

package com.jj.day7_videoview.demo1;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.jj.day7_videoview.R;

import java.io.File;

public class CamearImageActivity extends AppCompatActivity {
    private ImageView iv;
    private String path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camear_image);
        iv=(ImageView)findViewById(R.id.iv);
        path=getExternalCacheDir().getAbsolutePath()+ File.separator+"222.png";

    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                Intent intent=new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,200);
                break;
            case R.id.btn2:
                Intent intent2=new Intent();
                intent2.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                startActivityForResult(intent2,300);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200&&resultCode== Activity.RESULT_OK){
            Bundle bundle=data.getExtras();
            Bitmap bm=(Bitmap)bundle.get("data");
            if (bm!=null){
                iv.setImageBitmap(bm);
            }
        }else if (requestCode==300&&resultCode==Activity.RESULT_OK){
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inJustDecodeBounds=true;
            BitmapFactory.decodeFile(path,options);
            int outHeight=options.outHeight;
            int outWidth=options.outWidth;
            int sampleHeight=outHeight/200;
            int sampleWidth=outWidth/200;
            int size=Math.max(sampleHeight,sampleWidth);
            options.inSampleSize=size;

            options.inPreferredConfig=Bitmap.Config.RGB_565;
            options.inJustDecodeBounds=false;
            Bitmap bitmap=BitmapFactory.decodeFile(path,options);
            iv.setImageBitmap(bitmap);
        }
    }
}
     XML文件

     

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_camear_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jj.day7_videoview.demo1.CamearImageActivity">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转照相界面"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="跳转照相界面,保存到指定目录中"/>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@mipmap/ic_launcher"/>
</LinearLayout>
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值