Android调用系统相机拍照,从图库中选择照片,调用系统摄像机录像

最近在Android Studio上折腾代码,昨天编译器又很不给面子的报错了:

Error type 3
Error: Activity class {com.example.myapplication/com.example.myapplication
.MainActivity} does not exist.

程序都安装不起来,并不是忘了注册类名,在stackoverflow按照大神们的解决方法试了,没有效果。接下来所有新建的工程都报这个错误,搞得我都怀疑喵生了。。。都准备重装新版本了,1.5版本已出。
今天过来一看,好了,得出结论,重启是万能的:重启了电脑和手机。
先上布局:
这里用了一个ImageView,拍照和图库都用这个显示。显示录像的控件使用的是VideoView。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:text="拍照"
        android:onClick="takePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="从手机相册中选择"
        android:onClick="choosePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="#ff00ff"
        android:scaleType="fitXY"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="录制视频"
        android:onClick="takeVideo"/>
    <VideoView
        android:id="@+id/vv"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_gravity="center"/>
</LinearLayout>
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Context context;
    public static final String TAG="MyLog";
    private static final  int REQUEST_CAMERA_CAPTURE=1;
    private static final  int REQUEST_IMAGE_CAPTURE=2;
    private static final  int REQUEST_VIDEO_CAPTURE=3;
    private File imagePath,videoPath;
    private ImageView mImageView;
    private VideoView mVideoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        mImageView=(ImageView)findViewById(R.id.iv);
        mVideoView=(VideoView)findViewById(R.id.vv);
    }

    public void takePhoto(View view){
        takePhoto();
    }
    public void choosePhoto(View view){
        choosePhoto();
    }
    public void myCamera(View view){myCamera();}
    public void takeVideo(View view){
        takeVideo();
    }

    private void takeVideo(){
        if(hasSdcard()) {
            videoPath = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_MOVIES);
            if (!videoPath.exists()) {
                videoPath.mkdirs();
            }
            videoPath = new File(videoPath.getPath() + "/" + "test.mp4");
//        直接调用已经存在的录像并播放
//        Uri uri=Uri.fromFile(videoPath);
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        Log.d(TAG,uri+"");
//        intent.setDataAndType(uri, "video/mp4");
//        startActivity(intent);

            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (intent.resolveActivity(getPackageManager()) != null) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoPath));
                startActivityForResult(intent, REQUEST_VIDEO_CAPTURE);
            }
        }
    }

    private void takePhoto(){
        if(hasSdcard()) {
Environment.DIRECTORY_PICTURES);
            if (!imagePath.exists()) {
                imagePath.mkdirs();
            }
            imagePath = new File(imagePath.getPath() + "/" + "temp.png");
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (intent.resolveActivity(getPackageManager()) != null) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
                startActivityForResult(intent, REQUEST_CAMERA_CAPTURE);
            }
        }
    }

    private void choosePhoto(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_CAMERA_CAPTURE&&resultCode==RESULT_OK){
            FileInputStream fis = null;
            try {
                fis=new FileInputStream(imagePath);
                Bitmap bitmap= BitmapFactory.decodeStream(fis);
                mImageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else if(requestCode==REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){
            Uri uri=data.getData();
            mImageView.setImageURI(uri);
        }else if(requestCode==REQUEST_VIDEO_CAPTURE&&resultCode==RESULT_OK){
            Uri uri=Uri.fromFile(videoPath);
            Log.d(TAG,uri+"");
            mVideoView.setVideoURI(uri);
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.start();
            mVideoView.requestFocus();
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d(TAG, "完成");
                }
            });

            mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.d(TAG, "播放中出现错误");
                    return false;
                }
            });
        }
    }

private boolean hasSdcard(){
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            return true;
        }else{
            return false;
        }
    }
}

官方说明:
注意在调用startActivityForResult()方法之前,先调用resolveActivity(),这个方法会返回能处理该Intent的第一个Activity(译注:即检查有没有能处理这个Intent的Activity)。执行这个检查非常重要,因为如果在调用startActivityForResult()时,没有应用能处理你的Intent,应用将会崩溃。所以只要返回结果不为null,使用该Intent就是安全的。
最后不要忘了加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值