Use Bitmapfactory.decodestream (...) to create the Bitmap directly after taking the photo and show that there is a problem.
Bitmap is a simple object that stores only the actual pixel data, that is, even if the original photo has been compressed, but when you save the Bitmap object, the file does not compress as well, causing the picture to not display, so you need to scale the bitmap.
The scaling bitmap code is as follows:
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) {//R EAD 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) & Gt (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); } }
Take photos and display the code:
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:allo Wbackup= "true" android:icon= "@mipmap/ic_launcher" android:label= "@string/app_name" android:roundicon= "@mipmap/ic_launcher_round" android:supportsrtl= "true" android:theme= "@style/apptheme" > <activi Ty Android:name= ". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& Gt <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 "& Gt <button android:id= "@+id/take_photo_button" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:text= "Take a photo" android:layout_gravity= "Center_horizontal" Android:tex Tallcaps= "false"/> <button android:id= "@+id/choose_from_album_button" android:layout_width= "Wrap_c Ontent "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= "cent Er_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.onactivity Result (Requestcode, ResultCode, data); Switch (requestcode) {case MainActivity.CAMERA_INTENT_REQUEST_CODE:if (ResultCode = = Activity . RESULT_OK) {/*try {//Bitmap are too big to display 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) {th Is.handlepicturefromalbum (data); } break; Default:break; }} @Override public void Onrequestpermissionsresult (int. Requestcode, @NonNull string[] permissions, @NonNull in T[] grantresults) {Super.onrequestpermissionsresult (Requestcode, permissions, grantresults); Switch (requestcode) {case MainActivity.WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE:if (PE Rmissions.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 = 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 #, not with 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 (+) 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 = d Ocid.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 Contentu RI = 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)); } 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 photo or album Select photos to display scaled bitmap Demo