需求:从本地相册找图片,或通过调用系统相机拍照得到图片。
容易出错的地方:
1、当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量或者其他方式)然后设置给imageView
imageView.setImageURI(uri);
2、我发现手机前置摄像头拍出来的照片只有几百KB,直接用imageView.setImageURI(uri);没有很大问题,但是后置摄像头拍出来的照片比较大,这个时候使用imageView.setImageURI(uri);就容易出现 out of memory(oom)错误,我们需要先把URI转换为Bitmap,再压缩bitmap,然后通过imageView.setImageBitmap(bitmap);来显示图片。
3、将照片存放到SD卡中后,照片不能立即出现在系统相册中,因此我们需要发送广播去提醒相册更新照片。
4、这里用到了sharepreference,要注意用完之后移除缓存。
代码:
MainActivity:
package com.sctu.edu.test;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.sctu.edu.test.tools.ImageTools;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static final int PHOTO_FROM_GALLERY = 1;
private static final int PHOTO_FROM_CAMERA = 2;
private ImageView imageView;
private File appDir;
private Uri uriForCamera;
private Date date;
private String str = "";
private SharePreference sharePreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Android不推荐使用全局变量,我在这里使用了sharePreference
sharePreference = SharePreference.getInstance(this);
imageView = (ImageView) findViewById(R.id.imageView);
}
//从相册取图片
public void gallery(View view) {
Intent intent = new Intent();
intent.setType("image
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForCamera);
startActivityForResult(intent, PHOTO_FROM_CAMERA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//第一层switch
switch (requestCode) {
case PHOTO_FROM_GALLERY:
//第二层switch
switch (resultCode) {
case RESULT_OK:
if (data != null) {
Uri uri = data.getData();
imageView.setImageURI(uri);
}
break;
case RESULT_CANCELED:
break;
}
break;
case PHOTO_FROM_CAMERA:
if (resultCode == RESULT_OK) {
Uri uri = Uri.parse(sharePreference.getString("uri"));
updateDCIM(uri);
try {
//把URI转换为Bitmap,并将bitmap压缩,防止OOM(out of memory)
Bitmap bitmap = ImageTools.getBitmapFromUri(uri, this);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
removeCache("uri");
} else {
Log.e("result", "is not ok" + resultCode);
}
break;
default:
break;
}
}
private File createImageStoragePath() {
if (hasSdcard()) {
appDir = new File("/sdcard/testImage/");
if (!appDir.exists()) {
appDir.mkdirs();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
date = new Date();
str = simpleDateFormat.format(date);
String fileName = str + ".jpg";
File file = new File(appDir, fileName);
return file;
} else {
Log.e("sd", "is not load");
return null;
}
}
private void updateDCIM(Uri uri) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(uri);
this.sendBroadcast(intent);
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");
}
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
private void removeCache(String cache) {
if (sharePreference.ifHaveShare(cache)) {
sharePreference.removeOneCache(cache);
} else {
Log.e("this cache", "is not exist.");
}
}
}
ImageTools:
package com.sctu.edu.test.tools;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class ImageTools {
public static Bitmap getBitmapFromUri(Uri uri, Activity activity) throws IOException {
InputStream inputStream = activity.getContentResolver().openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
int originalWidth = options.outWidth;
int originalHeight = options.outHeight;
if (originalWidth == -1 || originalHeight == -1) {
return null;
}
float height = 800f;
float width = 480f;
int be = 1; //be=1表示不缩放
if (originalWidth > originalHeight && originalWidth > width) {
be = (int) (originalWidth / width);
} else if (originalWidth < originalHeight && originalHeight > height) {
be = (int) (originalHeight / height);
}
if (be <= 0) {
be = 1;
}
BitmapFactory.Options bitmapOptinos = new BitmapFactory.Options();
bitmapOptinos.inSampleSize = be;
bitmapOptinos.inDither = true;
bitmapOptinos.inPreferredConfig = Bitmap.Config.ARGB_8888;
inputStream = activity.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOptinos);
inputStream.close();
return compressImage(bitmap);
}
public static Bitmap compressImage(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
int options = 100;
while (byteArrayOutputStream.toByteArray().length / 1024 > 100) {
byteArrayOutputStream.reset();
//第一个参数 :图片格式 ,第二个参数: 图片质量,100为最高,0为最差 ,第三个参数:保存压缩后的数据的流
bitmap.compress(Bitmap.CompressFormat.JPEG, options, byteArrayOutputStream);
options -= 10;
}
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
Bitmap bitmapImage = BitmapFactory.decodeStream(byteArrayInputStream, null, null);
return bitmapImage;
}
}
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.sctu.edu.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature
android:name="android.hardware.camera"
android:required="true"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="com.miui.whetstone.permission.ACCESS_PROVIDER"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:background="#fff"
android:orientation="vertical"
tools:context="com.sctu.edu.test.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从图库找图片"
android:id="@+id/gallery"
android:onClick="gallery"
android:background="#ccc"
android:textSize="20sp"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照获取图片"
android:id="@+id/camera"
android:onClick="camera"
android:background="#ccc"
android:textSize="20sp"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:scaleType="fitXY"
android:background="@mipmap/ic_launcher"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
/>
</LinearLayout>
效果图:
或许有人会问,在Android6.0上面怎么点击拍照就出现闪退,那是因为我设置的最高SDK版本大于23,而我现在还没对运行时权限做处理,也许我会在下一篇博客里处理这个问题。谢谢浏览,希望对你有帮助!
您可能感兴趣的文章:基于Android实现保存图片到本地并可以在相册中显示出来android获取相册图片和路径的实现方法android照相、相册获取图片剪裁报错的解决方法解决Android从相册中获取图片出错图片却无法裁剪问题的方法Android实现读取相机(相册)图片并进行剪裁android中打开相机、打开相册进行图片的获取示例Android实现保存图片到本地并在相册中显示Android实现长按图片保存至相册功能Android 图片存储到指定路径和相册的方法Android实现选择相册图片并显示功能