版权声明:如果需要引用文中内容,希望可以带上相应的链接地址,万分感谢。地址: https://blog.csdn.net/shaowanyunBLOG/article/details/71126790
Android实现打开系统照相机&相册图片展示
Android系统本身提供了非常多的多媒体应用的API可供开发人员使用开发功能多彩多样的app,那么,因为之前自己完成的一个小小的app里面刚好我就用到了“打开系统照相机和打开相册”这两个部分的功能然后完成一个“图片空间”的这样一个子模块的功能。然后我接下来就会把我的代码给出来,然后大致上介绍一下怎样实现具体的功能。
1.主要功能java代码
(1)打开系统照相机
(2)打开相册及展示private Button btn_takephoto; btn_takephoto = (Button) findViewById(R.id.btn_takephoto); btn_takephoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); } });
private Button btn_choosephoto; private ImageView img; btn_choosephoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } }); protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); img.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } }
2.完整java代码
import com.example.mynote.R; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.content.ContextCompat; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class PhotoActivity extends Activity { protected static final int RESULT_LOAD_IMAGE = 0; private Button btn_takephoto, btn_choosephoto, photo_back; private ImageView img; @SuppressLint("ServiceCast") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shake); btn_takephoto = (Button) findViewById(R.id.btn_takephoto); btn_choosephoto = (Button) findViewById(R.id.btn_choosephoto); photo_back = (Button) findViewById(R.id.photo_back); img = (ImageView) findViewById(R.id.img); // 返回键设置点击事件 photo_back.setOnTouchListener((new View.OnTouchListener() { @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View view, MotionEvent ev) { // TODO Auto-generated method stub int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: photo_back.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.back_click)); break; case MotionEvent.ACTION_UP: photo_back.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.back)); Intent intent = new Intent(getApplicationContext(), ChooseActivity.class); startActivity(intent); } return false; } })); btn_takephoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction("android.media.action.IMAGE_CAPTURE"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); } }); btn_choosephoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); img.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } } }
3.布局文件
<RelativeLayout 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" tools:context="${relativePackage}.${activityClass}" > <RelativeLayout android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="55dp" android:background="#ff0000" > <Button android:id="@+id/photo_back" android:layout_width="45dp" android:layout_height="45dp" android:layout_marginTop="5dp" android:background="@drawable/back" > </Button> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="我的图片空间" android:textColor="#ffffff" android:textSize="25sp" /> </RelativeLayout> <View android:id="@+id/top1" android:layout_width="match_parent" android:layout_height="2dp" android:background="#ff0000" android:layout_below="@+id/rl" android:layout_marginTop="25dp"></View> <View android:id="@+id/top2" android:layout_width="match_parent" android:layout_height="2dp" android:background="#ff0000" android:layout_below="@+id/img" android:layout_marginTop="20dp"></View> <View android:id="@+id/left" android:layout_width="2dp" android:layout_height="match_parent" android:background="#ff0000" android:layout_toLeftOf="@+id/btn_takephoto" android:layout_marginLeft="5dp" android:layout_marginTop="25dp"></View> <View android:id="@+id/right" android:layout_width="2dp" android:layout_height="match_parent" android:background="#ff0000" android:layout_toRightOf="@+id/btn_takephoto" android:layout_marginTop="20dp"></View> <View android:id="@+id/bottom1" android:layout_width="match_parent" android:layout_height="2dp" android:background="#ff0000" android:layout_below="@+id/img" android:layout_marginTop="25dp"></View> <View android:id="@+id/bottom2" android:layout_width="match_parent" android:layout_height="2dp" android:background="#ff0000" android:layout_below="@+id/rl" android:layout_marginTop="20dp"></View> <Button android:id="@+id/btn_takephoto" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_below="@+id/rl" android:layout_marginTop="50dp" android:background="#ff0000" android:text="打开照相机" android:layout_centerHorizontal="true" android:textColor="#ffffff" android:textSize="20sp" > </Button> <Button android:id="@+id/btn_choosephoto" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_below="@+id/btn_takephoto" android:layout_marginTop="15dp" android:layout_centerHorizontal="true" android:background="#ff0000" android:text="从相册中选择照片" android:textColor="#ffffff" android:textSize="20sp" > </Button> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="340dp" android:layout_below="@+id/btn_choosephoto" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@drawable/img" > </ImageView> </RelativeLayout>