码迷,mamicode.com
首页 > 移动开发 > 详细

彻底解决android拍照后无法显示的问题

时间:2016-05-02 21:15:43      阅读:899      评论:0      收藏:0      [点我收藏+]

标签:

这是对上篇"android 图片拍照,相册选图,剪切并显示"的文章之后的 改进

上一篇文章虽然能解决图片的拍照剪切以及显示,但是发现他有一个缺点, 如果该程序单独运行,貌似没有任何问题, 但是把它整合到项目中后,发现有时在小米手机或者三星手机上面仍旧会失败, 原因可能是 拍照后导致内存不足,从而拍照后返回的时候,没有返回数据,导致图片显示失败,当然这是一定的概率事件. 

 

针对这一情况做如下改变,代码如下:

 

package com.kuiboo.xiaoyao.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.kuiboo.xiaoyao.R;
import com.kuiboo.xiaoyao.util.ActionSheetDialog;
import com.kuiboo.xiaoyao.util.CircleImageView;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Random;

public class EditMineActivity extends Activity {
    private static final String TAG = "MainActivity";
    private static final int TAKE_SMALL_PICTURE = 2;
    private static final int CROP_BIG_PICTURE = 3;
    private static final int CROP_SMALL_PICTURE = 4;
    private static final int CHOOSE_SMALL_PICTURE = 6;
    private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";
    private Uri imageUri;//存储图片的uri地址
    CircleImageView iv;//自定义圆形图片,或者使用第三方库,或者基本ImageView都可以
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_edit);
        Log.e("执行了几次","aaa");
        iv= (CircleImageView) findViewById(R.id.photoView);
        iv.setBorderWidth(5);
        imageUri = Uri.parse(IMAGE_FILE_LOCATION);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ActionSheetDialog(EditMineActivity.this).Builder()
                        .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.BULE, new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int witch) {
                                openCamera();
                            }
                        }).addSheetItem("打开相册", ActionSheetDialog.SheetItemColor.BULE, new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int witch) {
                        openPic();
                    }
                }).show();
            }
        });
    }

    //获取相册图片
    private void openPic() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 100);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, CHOOSE_SMALL_PICTURE);
    }

    //获取拍照小图片
    private void openCamera() {
        if(imageUri == null) {
            Log.e(TAG, "图片uri不能为空!");
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, TAKE_SMALL_PICTURE);
    }
    //剪切
    private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // 没有人脸识别
        startActivityForResult(intent, requestCode);
    }

    private Bitmap decodeUriAsBitmap(Uri uri){
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != Activity.RESULT_OK){
            Log.e(TAG, "requestCode = " + requestCode);
            Log.e(TAG, "resultCode = " + resultCode);
            Log.e(TAG, "data = " + data);
            return;
        }else{
            switch (requestCode) {
//                case CROP_BIG_PICTURE://剪切大图
//                    if(imageUri != null){
//                        Bitmap bitmap = decodeUriAsBitmap(imageUri);
//                        iv.setImageBitmap(bitmap);
//                    }
//                    break;
                case TAKE_SMALL_PICTURE://拍照
                    cropImageUri(imageUri, 260, 200, CROP_SMALL_PICTURE);
                    break;
                case CROP_SMALL_PICTURE://剪切小图
                    if(imageUri != null){
                        Bitmap bitmap = decodeUriAsBitmap(imageUri);
                        iv.setImageBitmap(bitmap);
                    }else{
                        Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
                    }
                    break;

                case CHOOSE_SMALL_PICTURE://相册选择
                    if(data != null){
                        Bitmap bitmap = data.getParcelableExtra("data");
                        iv.setImageBitmap(bitmap);
                    }else{
                        Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);
                    }
                    break;
            }
        }
    }
}

效果图就不上了 ,和上一篇文章效果图一样.

彻底解决android拍照后无法显示的问题

标签:

原文地址:http://www.cnblogs.com/android-zcq/p/5452977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!