您的位置:首页 > 其它

调取系统相册和照相机选取图片

2016-07-05 20:30 274 查看
在让用户自定义头像或皮肤的时候 可能会采取照相或选取相册等方法来实现用户自定义,这里我们就简单实现一下~

在这里先贴出布局-----仅供参考

activity_main://一张图片而已 很简单

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/img_true"
/>

activity_select_photo://选择图片 其中有一些图片资源和动画,在文末分享给大家

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >

<LinearLayout
android:id="@+id/dialog_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_photo_up_bg"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >

<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="@drawable/select_photo_bg"
android:text="@string/paizhaoxuanqu"
android:textStyle="bold" />

<View
android:layout_width="fill_parent"
android:layout_height="0.5px"
android:background="#828282" />

<Button
android:id="@+id/btn_pick_photo"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="0dip"
android:background="@drawable/select_photo_bg"
android:text="@string/xiangcexuanqu"
android:textStyle="bold" />
</LinearLayout>

<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="20dip"
android:background="@drawable/select_photo_bg"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="@string/exit"
android:textColor="#ffff0000"
android:textStyle="bold" />
</LinearLayout>

</RelativeLayout>


-----------MainActivity-----------

图片监听事件 点击跳转选择页面使用回调

<span style="white-space:pre">    </span>private ImageView imgtrue;
/** 选择文件 */
public static final int TO_SELECT_PHOTO = 1;

<span style="white-space:pre">        </span>//图片选择事件
imgtrue.setOnClickListener(new OnClickListener() {
Intent intent;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(this, SelectPhotoActivity.class);
startActivityForResult(intent, TO_SELECT_PHOTO);
intent = null;
}
});


回调方法 接收图片地址 设置给控件

@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) {
picPath = data.getStringExtra(SelectPhotoActivity.KEY_PHOTO_PATH);
Bitmap bm = BitmapFactory.decodeFile(picPath);
zoomBitmap = zoomBitmap(bm, 300, 300);
imgtrue.setBackgroundDrawable(new BitmapDrawable(bm));
}
super.onActivityResult(requestCode, resultCode, data);
}


将获取的图片按宽高进行缩放

/**
* 将原图按照指定的宽高进行缩放
*
* @param oldBitmap
* @param newWidth
* @param newHeight
* @return
*/
private Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {
int width = oldBitmap.getWidth();
int height = oldBitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(newBitmap, matrix, null);
return newBitmap;
}


------------------SelectPhotoActivity------------------

//图片选择

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SelectPhotoActivity extends Activity implements OnClickListener {
/** 使用照相机拍照获取图片 */
public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
/** 使用相册中的图片 */
public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
/** 开启相机 */
private Button btn_take_photo;
/** 开启图册 */
private Button btn_pick_photo;
/** 取消 */
private Button btn_cancel;
/** 获取到的图片路径 */
private String picPath;
private Intent lastIntent;
private Uri photoUri;
/** 从Intent获取图片路径的KEY */
public static final String KEY_PHOTO_PATH = "photo_path";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_photo);
btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) findViewById(R.id.btn_cancel);

lastIntent = getIntent();

btn_take_photo.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_take_photo : // 开启相机
takePhoto();
break;
case R.id.btn_pick_photo : // 开启图册
pickPhoto();
break;
case R.id.btn_cancel : // 取消操作
this.finish();
break;
default :
break;
}
}
private void takePhoto() {
//  执行拍照前,应该先判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
/***
* 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图
* 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰
*/
ContentValues values = new ContentValues();
photoUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(getApplicationContext(), "内存卡不存在",
Toast.LENGTH_SHORT).show();
}
}

/***
*  从相册中取图片
*/
private void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return super.onTouchEvent(event);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
doPhoto(requestCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}

/**
* 选择图片后,获取图片的路径
*/
private void doPhoto(int requestCode, Intent data) {
if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {// 从相册取图片,有些手机有异常情况,请注意
if (data == null) {
Toast.makeText(getApplicationContext(), "选择图片文件出错",
Toast.LENGTH_SHORT).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(getApplicationContext(), "选择图片文件出错",
Toast.LENGTH_SHORT).show();
return;
}
}
String[] pojo = {MediaStore.Images.Media.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
cursor.close();
}
if (picPath != null
&& (picPath.endsWith(".png") || picPath.endsWith(".PNG")
|| picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
setResult(Activity.RESULT_OK, lastIntent);
finish();
} else {
Toast.makeText(getApplicationContext(), "选择图片文件不正确",
Toast.LENGTH_SHORT).show();
}
}

}


资源分享:


动画anim

push_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />

</set>



push_bottom_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p" />
</set>


图片资源:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: