Android 拍照或相簿選擇照片進行顯示縮放位元影像 Demo

來源:互聯網
上載者:User

標籤:label   ble   download   .net   tco   data   sam   dia   equals   

拍照後直接使用 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.