Android实现打开系统照相机&相册图片展示
2017-05-03 17:11
375 查看
Android实现打开系统照相机&相册图片展示
Android系统本身提供了非常多的多媒体应用的API可供开发人员使用开发功能多彩多样的app,那么,因为之前自己完成的一个小小的app里面刚好我就用到了“打开系统照相机和打开相册”这两个部分的功能然后完成一个“图片空间”的这样一个子模块的功能。然后我接下来就会把我的代码给出来,然后大致上介绍一下怎样实现具体的功能。
1.主要功能java代码
(1)打开系统照相机
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);
}
}); (2)打开相册及展示
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.AC
9c6f
TION_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>
Android系统本身提供了非常多的多媒体应用的API可供开发人员使用开发功能多彩多样的app,那么,因为之前自己完成的一个小小的app里面刚好我就用到了“打开系统照相机和打开相册”这两个部分的功能然后完成一个“图片空间”的这样一个子模块的功能。然后我接下来就会把我的代码给出来,然后大致上介绍一下怎样实现具体的功能。
1.主要功能java代码
(1)打开系统照相机
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);
}
}); (2)打开相册及展示
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.AC
9c6f
TION_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>
相关文章推荐
- Android基础--调用系统相机,打开相册,裁剪图片并展示到界面
- Android基础--调用系统相机,打开相册,展示到界面,出现的一些问题Uri为空,data为null,图片错乱问题
- 实现打开Android系统相册、照相机并实现照相,录像功能。
- Android调用系统相机,相册裁切图片展示的实现
- Android实现类似QQ头像点击打开相册或者拍照,并获取裁剪后的图片
- Android调用系统相机,相册,并实现图片剪裁功能
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android 打开照相机、获取相册图片、获取图片并裁减
- android调取系统相册和照相机选取图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- android 打开系统相册得到路径 上传图片到php服务器
- Android调用相册拍照控件实现系统控件缩放切割图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android 通过开源框架实现加载网络图片并下载到SD卡通知系统相册显示,(并实现分享图片功能)
- android实现ViewPager,打开app展示图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- Android 调用相册 拍照 实现系统控件缩放 切割图片
- iOS-打开照相机&打开本地相册&图片上传