有时朋友圈发一条状态,想要添加一张照片我们可以直接用相机拍摄,也可以直接在相册中选取上传,这就是用到了应用调用相机或者相册的功能。我们如何为应用添加这个功能呢?
调用相机拍照
对于使用相机进行拍摄实现的方式有很多种,可以直接在应用中自己定义一个Camera,也可以调用系统的相机。这里我们只学习使用隐式调用相机的方法。
1. 首先定义布局,Button用于启动系统的相机,ImageView用于显示拍摄的图片。
<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=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_start_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动系统相机"/>
</RelativeLayout>
2. 在Activity中通过Intent隐式启动相机。
public class MainActivity extends AppCompatActivity {
private static final int PICTURE_FROM_CAMERA = 0X32;
private static final int PICTURE_FROM_GALLERY = 0X34;
private Button mButtonStart;//启动相机的按钮
private Button mButtonOpen;//启动相册的按钮
private ImageView mImageView;//显示图片
private File file;//存储拍摄图片的文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonStart = (Button) findViewById(R.id.button_start_camera);
mButtonOpen = (Button) findViewById(R.id.button_open_gallery);
mImageView = (ImageView) findViewById(R.id.imageview);
//点击按钮启动相机
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//启动相机的Action
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//文件的保存位置
file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//设置图片拍摄后保存的位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//启动相机,这里使用有返回结果的启动
startActivityForResult(intent, PICTURE_FROM_CAMERA);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICTURE_FROM_CAMERA:
//这里对图片进行了压缩,因为有些手机拍摄的照片过大,无法显示到ImageView中,所以我们将图片近行了压缩然后在进行显示
ZipImage.zipImage(Uri.fromFile(file).getPath());
//将图片设置到ImageView中,这里使用setImageURI()方法进行设置。
mImageView.setImageURI(Uri.fromFile(file));
break;
}
}
}
}
这里我们对拍摄获得的图片进行了压缩处理,因为有些手机拍摄的图片过大,在ImageView中无法正常显示(当时在做练习的时候就出现了这个问题),所以需要压缩一下。这里我们使用下面的方法进行压缩,调用ZipImage 的静态方法zipImage(String savePath)即可。
public class ZipImage {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
3. 这里使用到了对我们标准SD卡的读写操作,所以我们需要加权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.camerademo" >
<!-- 读写标准SD卡的权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
由于模拟器上的相机是无法使用的,因此我们不在贴出结果。
使用相册选取
有时候我们想使用相册里的图片,这时我们就需要调用相册了。我们看下面的步骤……
1. 首先定义布局,Button用于启动系统的相册,ImageView用于显示选择的图片。
<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=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_open_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动系统相册"/>
</RelativeLayout>
2. 在Activity中通过Intent隐式启动系统相册。
public class MainActivity extends AppCompatActivity {
private static final int PICTURE_FROM_GALLERY = 0X34;
private Button mButtonOpen;//启动相册的按钮
private ImageView mImageView;//显示图片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonOpen = (Button) findViewById(R.id.button_open_gallery);
mImageView = (ImageView) findViewById(R.id.imageview);
mButtonOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//设置启动相册的Action
intent.setAction(Intent.ACTION_GET_CONTENT);
//设置类型
intent.setType("image/*");
//启动相册,这里使用有返回结果的启动
startActivityForResult(intent, PICTURE_FROM_GALLERY);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICTURE_FROM_GALLERY:
//通过返回的data数据,获取图片的路径信息,但是这个路径是Uri的。
Uri uri = data.getData();
//我们要压缩进行压缩首先要将Uri地址转换为真实路径。
File file = getFilePath(uri);
//压缩图片
ZipImage.zipImage(file.getAbsolutePath());
mImageView.setImageURI(Uri.fromFile(file));
break;
}
}
}
@NonNull
private File getFilePath(Uri uri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
return new File(img_path);
}
}
由于我们通过返回的data数据获取图片的路径信息是Uri的,我们没办法应用到压缩图片的方法中,我们需要先将这个Uri的路径转化为真实的路径。这里我们使用下面这个方法获得uri的真实路径:
@NonNull
private File getFilePath(Uri uri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
return new File(img_path);
}
获得真实路径后在对其进行压缩,然后显示。压缩的方法在前面已经列出来了,所以这里不再重复……