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

Stella981
• 阅读 366
package com.only.android.app;
 
import java.io.File;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
import com.only.android.R;
 
public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
    private Button selectImageBtn;
    private ImageView imageView;
     
    private File sdcardTempFile;
    private AlertDialog dialog;
    private int crop = 180;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagescale);
 
        selectImageBtn = (Button) findViewById(R.id.selectImageBtn);
        imageView = (ImageView) findViewById(R.id.imageView);
 
        selectImageBtn.setOnClickListener(this);
        sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg");
 
    }
 
    @Override
    public void onClick(View v) {
        if (v == selectImageBtn) {
            if (dialog == null) {
                dialog = new AlertDialog.Builder(this).setItems(new String[] { "相机", "相册" }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) {
                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                            intent.putExtra("output", Uri.fromFile(sdcardTempFile));
                            intent.putExtra("crop", "true");
                            intent.putExtra("aspectX", 1);// 裁剪框比例
                            intent.putExtra("aspectY", 1);
                            intent.putExtra("outputX", crop);// 输出图片大小
                            intent.putExtra("outputY", crop);
                            startActivityForResult(intent, 101);
                        } else {
                            Intent intent = new Intent("android.intent.action.PICK");
                            intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
                            intent.putExtra("output", Uri.fromFile(sdcardTempFile));
                            intent.putExtra("crop", "true");
                            intent.putExtra("aspectX", 1);// 裁剪框比例
                            intent.putExtra("aspectY", 1);
                            intent.putExtra("outputX", crop);// 输出图片大小
                            intent.putExtra("outputY", crop);
                            startActivityForResult(intent, 100);
                        }
                    }
                }).create();
            }
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK) {
            Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
            imageView.setImageBitmap(bmp);
        }
    }
}
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java 帮助类
package com.quincy.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.
Stella981 Stella981
2年前
POI创建Excel
这个是项目中用到的时候,做的一个类,package com.topwalk.iwp.pluging;import java.awt.Color;import java.io.File;import java.io.FileOutputStream;import java.io.IOExceptio
Stella981 Stella981
2年前
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
Stella981 Stella981
2年前
Flutter BottomSheet底部弹窗效果
BottomSheet是一个从屏幕底部滑起的列表(以显示更多的内容)。你可以调用showBottomSheet()或showModalBottomSheet弹出import'package:flutter/material.dart';import'dart:async';classBottomSheetDem
Stella981 Stella981
2年前
None of the configured nodes are available
1.今天写了一段ES的测试代码,如下:package elasticSearch;import java.net.InetSocketAddress;import java.util.ArrayList;import java.util.List;import java.util.M
Stella981 Stella981
2年前
Base64工具类
package com.locator.encryption;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStr
Stella981 Stella981
2年前
Android Intent隐式意图显示菜单
package com.example.mytest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { @
Stella981 Stella981
2年前
Gallery实现流畅的新闻滚动 方法复写
package com.ename.views;import android.content.Context;import android.util.AttributeSet;import android.view.KeyEvent;import android.view.MotionEvent;
Stella981 Stella981
2年前
Android GridView 添加 HeadView
package com.example.test;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;i
Stella981 Stella981
2年前
Android图片处理工具类(持续积累补充)
package cn.mucang.android.community.utils;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;impo