package com.example.lianxi; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; public class MainActivity extends AppCompatActivity { private Button bu1,bu2; private ImageView imageView; private String path=Environment.getExternalStorageDirectory()+"/1603A.png"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bu1=findViewById(R.id.bu); bu2=findViewById(R.id.bu2); imageView=findViewById(R.id.img); bu1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path))); startActivityForResult(intent,1000); } }); bu2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent,3000); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==1000&&resultCode==RESULT_OK){ Intent intent=new Intent("com.android.camera.action.CROP"); intent.setDataAndType(Uri.fromFile(new File(path)),"image/*"); intent.putExtra("crop",true); intent.putExtra("outputX",250); intent.putExtra("outputY",250); intent.putExtra("aspectX",2); intent.putExtra("aspectY",1); intent.putExtra("return-data",true); startActivityForResult(intent,2000); } if (requestCode==2000&&resultCode==RESULT_OK){ Bitmap bitmap=data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); } if (requestCode==3000&&resultCode==RESULT_OK){ Uri uri = data.getData(); Intent intent=new Intent("com.android.camera.action.CROP"); intent.putExtra("crop",true); intent.putExtra("aspectX",2); intent.putExtra("aspectY",1); intent.putExtra("outputX",250); intent.putExtra("outputY",250); intent.setDataAndType(uri,"image/*"); intent.putExtra("return-data",true); startActivityForResult(intent,4000); } if (requestCode==4000&&resultCode==RESULT_OK){ Bitmap bitmap=data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); } } }
相机相册
最新推荐文章于 2021-04-08 00:03:02 发布