拍照后直接使用 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. (C#) 线程之 AutoResetEvent, EventHandle.

    AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...

  2. DirectX HLSL 内置函数

    Intrinsic Functions (DirectX HLSL) The following table lists the intrinsic functions available in HL ...

  3. 《ArcGIS Runtime SDK for Android开发笔记》——(3)、ArcGIS Runtime SDK概述

    1.前言 ArcGIS Runtime SDK是一整套用于构建原生及跨平台的地图应用程序的开发包,包括移动设备的Android.iOS.Windows Phone,针对桌面的.Net.Java.OSX ...

  4. Azure:陪伴你们,是我最长情的告白

    立即访问http://market.azure.cn

  5. 在线图片转base64

    function ImgToBase64(url, callback, outputFormat) { // outputFormat 用于指定输出格式的,遵循 MIME 标准 var canvas ...

  6. 拼接sql语句时拼接空字符串报sql错误

    先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...

  7. thinkphp的find()方法获取结果

    find方法返回的是一行记录,结果是一个数组,数组的key和sql中的field相对应,假设: $res=$model->find(filed="a,b,c"); 获取结果中 ...

  8. 【CSS】等高布局

    1. 负margin:   margin-bottom:-3000px; padding-bottom:3000px; 再配合父标签的overflow:hidden属性即可实现高度自动相等的效果.   ...

  9. C++ Eigen库和Matlab对比

    // A simple quickref for Eigen. Add anything that's missing. // Main author: Keir Mierle #include &l ...

  10. 2018.7.22 Jdom与dom的区别

    SAX 优点:①无需将整个文档加载到内存,因而内存消耗少 ②推模型允许注册多个ContentHandler 缺点:①没有内置的文档导航支持 ②不能够随机访问XML文档 ③不支持在原地修改XML ④不支 ...