拍照后直接使用 BitmapFactory.decodeStream(...) 进行创建 Bitmap 并显示是有问题的。
Bitmap 是个简单对象,它只存储实际像素数据,也就是说,即使原始照片已压缩过,但存入 Bitmap 对象时,文件并不会同样压缩,导致图片无法显示,所以需要进行缩放位图。
缩放位图代码如下:
PictureUtils.java
package club.seliote.camerademo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import java.io.File;
public class PictureUtils {
public static Bitmap getScaledBitmap(Uri uri, int destWidth, int destHeight) {
// read in the dimensions of the image on disk
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uri.getPath(), options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
// figure out how much to scale down by
int inSampleSize = 1;
if (srcWidth > destHeight || srcHeight > destHeight) {
inSampleSize = Math.round((srcWidth / destWidth) > (srcHeight / destHeight) ? (srcWidth / destWidth) : (srcHeight / destHeight));
}
// create a bitmap
options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(uri.getPath(), options);
}
public static Bitmap getScaledBitmap(Uri uri, Activity activity) {
Point point = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(point);
return PictureUtils.getScaledBitmap(uri, point.x, point.y);
}
}
拍照并显示的代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="club.seliote.camerademo">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="club.seliote.camerademo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
activity_main.xml
<?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:orientation="vertical">
<Button
android:id="@+id/take_photo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a photo"
android:layout_gravity="center_horizontal"
android:textAllCaps="false"/>
<Button
android:id="@+id/choose_from_album_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose from album"
android:layout_gravity="center_horizontal"
android:textAllCaps="false"/>
<ImageView
android:id="@+id/picture_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
xml/file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android" >
<external-path
name="images"
path="." />
</paths>
MainActivity.java
package club.seliote.camerademo;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_INTENT_REQUEST_CODE = 0;
private static final int ALBUM_INTENT_REQUEST_CODE = 1;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 0;
private String mPictureName;
private Uri mPictureUri;
private Button mTakePhotoButton;
private Button mChooseFromAlbum;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
mTakePhotoButton = this.findViewById(R.id.take_photo_button);
mChooseFromAlbum = this.findViewById(R.id.choose_from_album_button);
mImageView = this.findViewById(R.id.picture_image_view);
mTakePhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.takePhoto();
}
});
mChooseFromAlbum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MainActivity.WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
} else {
MainActivity.this.openAlbum();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case MainActivity.CAMERA_INTENT_REQUEST_CODE :
if (resultCode == Activity.RESULT_OK) {
/*try {
// Bitmap is too big to display in screen
Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(mPictureUri));
mImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException exp) {
exp.printStackTrace();
}*/
mImageView.setImageBitmap(PictureUtils.getScaledBitmap(mPictureUri, this));
}
break;
case MainActivity.ALBUM_INTENT_REQUEST_CODE :
if (resultCode == Activity.RESULT_OK) {
this.handlePictureFromAlbum(data);
}
break;
default :
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MainActivity.WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE :
if (permissions.length > 0 && permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
this.openAlbum();
} else {
Toast.makeText(this, "Permisson deny!", Toast.LENGTH_LONG).show();
}
}
}
private void takePhoto() {
// create file
File file = new File(this.getExternalCacheDir(), mPictureName = (System.currentTimeMillis() + ".jpg"));
try {
if (file.exists()) {
file.delete();
}
file.createNewFile();
} catch (IOException exp) {
exp.printStackTrace();
}
// wrap File to Uri
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// if version more then API 24, not use true path
mPictureUri = FileProvider.getUriForFile(this, "club.seliote.camerademo.fileprovider", file);
} else {
mPictureUri = Uri.fromFile(file);
}
// start camera activity
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPictureUri);
startActivityForResult(intent, MainActivity.CAMERA_INTENT_REQUEST_CODE);
}
private void openAlbum() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, MainActivity.ALBUM_INTENT_REQUEST_CODE);
}
// only for Android KitKat(4.4) or later
@TargetApi(19)
private void handlePictureFromAlbum(Intent intent) {
String imagePath = null;
Uri uri = intent.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.media.documents".equalsIgnoreCase(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "=" + id;
imagePath = this.getPathFormUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equalsIgnoreCase(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
imagePath = getPathFormUri(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
imagePath = this.getPathFormUri(uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
imagePath = uri.getPath();
}
if (imagePath != null) {
mImageView.setImageBitmap(PictureUtils.getScaledBitmap(Uri.fromFile(new File(imagePath)), this));
} else {
Toast.makeText(this, "Filed to get image", Toast.LENGTH_LONG).show();
}
}
@Nullable
// use uri get picture true path
private String getPathFormUri(Uri uri, String selection) {
String path = null;
Cursor cursor = this.getContentResolver().query(uri, null, selection, null, null);
if (cursor != null && cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return path;
}
}