Android从相机或相册获取图片裁剪

1package com.only.android.app; 2  3import java.io.File; 4  5import android.app.Activity; 6import android.app.AlertDialog; 7import android.content.DialogInterface; 8import android.content.Intent; 9import android.graphics.Bitmap; 10import android.graphics.BitmapFactory; 11import android.net.Uri; 12import android.os.Bundle; 13import android.os.SystemClock; 14import android.provider.MediaStore; 15import android.view.View; 16import android.widget.Button; 17import android.widget.ImageView; 18  19import com.only.android.R; 20  21public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener { 22    /** Called when the activity is first created. */ 23    private Button selectImageBtn; 24    private ImageView imageView; 25      26    private File sdcardTempFile; 27    private AlertDialog dialog; 28    private int crop = 180; 29  30    @Override 31    public void onCreate(Bundle savedInstanceState) { 32        super.onCreate(savedInstanceState); 33        setContentView(R.layout.imagescale); 34  35        selectImageBtn = (Button) findViewById(R.id.selectImageBtn); 36        imageView = (ImageView) findViewById(R.id.imageView); 37  38        selectImageBtn.setOnClickListener(this); 39        sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg"); 40  41    } 42  43    @Override 44    public void onClick(View v) { 45        if (== selectImageBtn) { 46            if (dialog == null) { 47                dialog = new AlertDialog.Builder(this).setItems(new String[] { "相机", "相册" }, new DialogInterface.OnClickListener() { 48                    @Override 49                    public void onClick(DialogInterface dialog, int which) { 50                        if (which == 0) { 51                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 52                            intent.putExtra("output", Uri.fromFile(sdcardTempFile)); 53                            intent.putExtra("crop", "true"); 54                            intent.putExtra("aspectX", 1);// 裁剪框比例 55                            intent.putExtra("aspectY", 1); 56                            intent.putExtra("outputX", crop);// 输出图片大小 57                            intent.putExtra("outputY", crop); 58                            startActivityForResult(intent, 101); 59                        } else { 60                            Intent intent = new Intent("android.intent.action.PICK"); 61                            intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); 62                            intent.putExtra("output", Uri.fromFile(sdcardTempFile)); 63                            intent.putExtra("crop", "true"); 64                            intent.putExtra("aspectX", 1);// 裁剪框比例 65                            intent.putExtra("aspectY", 1); 66                            intent.putExtra("outputX", crop);// 输出图片大小 67                            intent.putExtra("outputY", crop); 68                            startActivityForResult(intent, 100); 69                        } 70                    } 71                }).create(); 72            } 73            if (!dialog.isShowing()) { 74                dialog.show(); 75            } 76        } 77    } 78  79    @Override 80    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 81        if (resultCode == RESULT_OK) { 82            Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath()); 83            imageView.setImageBitmap(bmp); 84        } 85    } 86}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java 帮助类

package com.quincy.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.

POI创建Excel

这个是项目中用到的时候,做的一个类,package com.topwalk.iwp.pluging;import java.awt.Color;import java.io.File;import java.io.FileOutputStream;import java.io.IOExceptio

POI导入大excel文件

package me.shanzhi.test;import java.io.InputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.apac

Flutter BottomSheet底部弹窗效果

BottomSheet是一个从屏幕底部滑起的列表(以显示更多的内容)。你可以调用showBottomSheet()或showModalBottomSheet弹出import'package:flutter/material.dart';import'dart:async';classBottomSheetDem

Base64工具类

package com.locator.encryption;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStr