Android开发学习
Android开发中,需要启用手机自带相机进行拍照
新建布局文件:activity_main.xml,使用ImageView存储图片,代码内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center"
android:orientation="vertical" >
<ImageView
android:id="@+id/btn_image"
android:layout_width="145dip"
android:layout_height="145dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/app_panel_friendcard_icon" />
</LinearLayout>
实现的MainActivity.java代码如下:
package com.zhengdecai.camerapic;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
/**
* 调用相机照相功能
*
* @author 郑德才
*
*/
public class MainActivity extends Activity {
private ImageView photo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photo = (ImageView) this.findViewById(R.id.btn_image);
// 把文字控件添加监听,点击弹出自定义窗口
photo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 使用startActivityForResult启动SelectPicPopupWindow当返回到此Activity的时候就会调用onActivityResult函数
startActivityForResult(new Intent(MainActivity.this,
SelectPicPopupWindow.class), 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 1:
if (data != null) {
// 取得返回的Uri,基本上选择照片的时候返回的是以Uri形式,但是在拍照中有得机子呢Uri是空的,所以要特别注意
Uri mImageCaptureUri = data.getData();
// 返回的Uri不为空时,那么图片信息数据都会在Uri中获得。如果为空,那么我们就进行下面的方式获取
if (mImageCaptureUri != null) {
Bitmap image;
try {
// 这个方法是根据Uri获取Bitmap图片的静态方法
image = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), mImageCaptureUri);
BitmapDrawable photoDw = new BitmapDrawable(image);
if (image != null) {
// photo.setImageBitmap(image);
photo.setBackgroundDrawable(photoDw);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Bundle extras = data.getExtras();
if (extras != null) {
// 这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
Bitmap image = extras.getParcelable("data");
BitmapDrawable photoDw = new BitmapDrawable(image);
if (image != null) {
// photo.setImageBitmap(image);
photo.setBackgroundDrawable(photoDw);
}
}
}
}
break;
default:
break;
}
}
/**
* 保存图片到SD卡
*
* @param bitmap
* @param path
* @param fileName
* @return
*/
public static String savePicToSdcard(Bitmap bitmap, String path,
String fileName) {
String filePath = "";
if (bitmap == null) {
return filePath;
} else {
filePath = path + fileName;
File destFile = new File(filePath);
OutputStream os = null;
try {
os = new FileOutputStream(destFile);
bitmap.compress(CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (IOException e) {
filePath = "";
}
}
return filePath;
}
private static final int PHOTO_RESOULT = 3;// 结果
private static final String IMAGE_UNSPECIFIED = "image/*";
/**
* 收缩图片
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_RESOULT);
}
}
点击图片后弹出层布局文件及实现代码:
<?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"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从相册选择"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
package com.zhengdecai.camerapic;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* 弹出选择层
*
* @author 郑德才
*
*/
public class SelectPicPopupWindow extends Activity implements OnClickListener {
private Button btn_take_photo, btn_pick_photo, btn_cancel;
private LinearLayout layout;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
intent = getIntent();
btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
layout = (LinearLayout) findViewById(R.id.pop_layout);
// 添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity
layout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
}
});
// 添加按钮监听
btn_cancel.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_take_photo.setOnClickListener(this);
}
// 实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
//选择完或者拍完照后会在这里处理,然后我们继续使用setResult返回Intent以便可以传递数据和调用
if (data.getExtras() != null)
intent.putExtras(data.getExtras());
if (data.getData()!= null)
intent.setData(data.getData());
setResult(1, intent);
finish();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_take_photo:
try {
//拍照我们用Action为MediaStore.ACTION_IMAGE_CAPTURE,
//有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btn_pick_photo:
try {
//选择照片的时候也一样,我们用Action为Intent.ACTION_GET_CONTENT,
//有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 2);
} catch (ActivityNotFoundException e) {
}
break;
case R.id.btn_cancel:
finish();
break;
default:
break;
}
}
}
效果图:
评论列表: