拍照后直接使用 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;
}
}

Android 拍照或相册选择照片进行显示缩放位图 Demo的更多相关文章

  1. Swift - 使用UIImagePickerController从相册选择照片并展示

    1,UIImagePickerController介绍 (1)选择相册中的图片或者拍照,都是通过UIImagePickerController控制器实例化一个对象,然后通过self.presentVi ...

  2. HTML5 Plus 拍照或者相册选择图片上传

    HBuilder+HTML5 Plus+MUI实现拍照或者相册选择图片上传,利用HTML5 Plus的Camera.Gallery.IO.Storage和Uploader来实现手机APP拍照或者从相册 ...

  3. android——拍照,相册图片剪切其实就这么简单

    接触android这么久了.还没有真正的浩浩看看android拍照,相册图片剪切到底是怎么回事,每次都是从别人的代码一扣,就过来了.其实,谷歌提供的API已经很强大.只需要用的好,就那么几句就可以搞定 ...

  4. 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传

    一.Flutter image_picker 实现相机拍照和相册选择   https://pub.dev/packages/image_picker   二.Flutter 上传图片到服务器   ht ...

  5. Android拍照与相册选取图片

    做过几次拍照,相册选取图片,但都记不住,这次发表个简单的保存下 private static final int PHOTO_GRAPH = 1;// 拍照 private static final ...

  6. android 拍照或者图库选择 压缩后 图片 上传

    通过拍照或者从相册里选择图片通过压缩并上传时很多应用的常用功能,记录一下实现过程 一:创建个临时文件夹用于保存压缩后需要上传的图片 /** * path:存放图片目录路径 */ private Str ...

  7. Cocos2d-x使用android拍照功能加载照片内存过大,通过另存照片尺寸大小解决

    使用2dx调用android拍照功能,拍照结束后在2dx界面显示拍照照片,如果不对照片做处理,会出现内存过大的问题,导致程序崩溃,如果仅仅另存拍照照片,则照片质量大小均下降,导致照片不够清晰,后来发现 ...

  8. [Android] 拍照、截图、保存并显示在ImageView控件中

    近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法.     PS:作者购买 ...

  9. android 开启本地相册选择图片并返回显示

    .java package com.jerry.crop; import java.io.File; import android.app.Activity; import android.conte ...

随机推荐

  1. php的yii框架开发总结5

    MVC架构之model类: 我的日报系统用到的数据表:tbl_dailyreport表 其中anthor_id是外键,对应tbl_user数据表的主键id,下面是tbl_user表 class Dai ...

  2. 【转载】SQL执行计划

    要理解执行计划,怎么也得先理解,那各种各样的名词吧.鉴于自己还不是很了解.本文打算作为只写懂的,不懂的懂了才写. 在开头要先说明,第一次看执行计划要注意,SQL Server的执行计划是从右向左看的. ...

  3. C#中RichTextBox字体不统一(中英文)

    this.richTextBox1.Font = new System.Drawing.Font("微软雅黑", 12F);// new System.Drawing.Font(& ...

  4. leetcode:查找

    1.  word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...

  5. java线程详细版(未完待续)

    1. Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一 ...

  6. 为什么CRM Opportunity的删除会触发一个通向BW系统的RFC

    今天工作时我发现,我在SE38里用函数CRM_ORDER_DELETE删除一个Opportunity,居然弹出下图这个SAP Logon的屏幕,要连接BR1.这是什么鬼?! 查了一下,BR1是BW系统 ...

  7. QT Creater 配色方案及下载

    打开QT Creater的工具--选项--文本编辑器--字体和颜色,复制一份配色方案:Vim (dark) ->Vim (dark) (copy) 更改想更改的任何内容的配色.其中,修改后的文件 ...

  8. vuejs动态组件和v-once指令

    场景,点击某个按钮,两个子组件交替显示 <div id='root'> <child-one v-if='type==="child-one"'></ ...

  9. 表单(三):select

    选择框的value属性 <select name='location' id='selLocation'> <option value='Sunnyvale,Ca'>Sunny ...

  10. 2017.10.14 Java的流程控制语句switch&&随机点名器

    今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构     * A:switch语句解构       * a:switch只能针对某个表达式的值作 ...