拍照或从图库选择

源码:

MainActivity.java:

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final int PHOTO_REQUEST_CAREMA = 1;//take photo
   private static final int PHOTO_REQUEST_GALLERY = 2;//take from gallary
   private static final int PHOTO_REQUEST_CUT = 3;//crop
   private ImageView iv_image;
   private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
   private File tempFile;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       this.iv_image = (ImageView) this.findViewById(R.id.iv_image);
       
       
   }
   public void gallery(View view) {
       Intent intent = new Intent(Intent.ACTION_PICK);
       intent.setType("image/*");
       startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
   }


   public void camera(View view) {
       Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
       if (hasSdcard()) {
           tempFile = new File(Environment.getExternalStorageDirectory(),
                   PHOTO_FILE_NAME);
           Uri uri = Uri.fromFile(tempFile);
           intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
       }
       startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
   }


   private void crop(Uri uri) {
       Intent intent = new Intent("com.android.camera.action.CROP");
       intent.setDataAndType(uri, "image/*");
       intent.putExtra("crop", "true");
       intent.putExtra("aspectX", 1);
       intent.putExtra("aspectY", 1);
       intent.putExtra("outputX", 250);
       intent.putExtra("outputY", 250);


       intent.putExtra("outputFormat", "JPEG");
       intent.putExtra("noFaceDetection", true);
       intent.putExtra("return-data", true);
       startActivityForResult(intent, PHOTO_REQUEST_CUT);
   }
   private boolean hasSdcard() {
       if (Environment.getExternalStorageState().equals(
               Environment.MEDIA_MOUNTED)) {
           return true;
       } else {
           return false;
       }
   }


   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == PHOTO_REQUEST_GALLERY) {
           if (data != null) {
               Uri uri = data.getData();
               crop(uri);
           }


       } else if (requestCode == PHOTO_REQUEST_CAREMA) {
           if (hasSdcard()) {
               crop(Uri.fromFile(tempFile));
           } else {
               Toast.makeText(MainActivity.this, "scard wrong", Toast.LENGTH_SHORT).show();
           }


       } else if (requestCode == PHOTO_REQUEST_CUT) {
           if (data != null) {
               Bitmap bitmap = data.getParcelableExtra("data");
               this.iv_image.setImageBitmap(bitmap);
           }
           try {
               tempFile.delete();
           } catch (Exception e) {
               e.printStackTrace();
           }


       }


       super.onActivityResult(requestCode, resultCode, data);
   }

}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="gallery"
        android:text="获取图库图片" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="camera"
        android:text="拍照获取图片" />


    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值