LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:tools="
http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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>
//------------------æçMainActivity --------------ä¹å¾ç®å--------------------------
package tackpicture.bwie.com.tackpicture;
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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private ImageView iv_image;
private static final int PHOTO_REQUEST_CAREMA = 1;// æç
§
private static final int PHOTO_REQUEST_GALLERY = 2;// ä»ç¸åä¸éæ©
private static final int PHOTO_REQUEST_CUT = 3;// ç»æ
/* 头åå称 */
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);
//æ¾å°æ§ä»¶
iv_image = (ImageView) findViewById(R.id.iv_image);
}
//å¾åº
public void camera(View view) {
// æ¿æ´»ç³»ç»å¾åºï¼éæ©ä¸å¼ å¾ç
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// å¼å¯ä¸ä¸ªå¸¦æè¿åå¼çActivityï¼è¯·æ±ç 为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
//ç¸æº
public void gallery(View view) {
// æ¿æ´»ç¸æº
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// å¤æåå¨å¡æ¯å¦å¯ä»¥ç¨ï¼å¯ç¨è¿è¡åå¨
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
// ä»æ件ä¸å建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// å¼å¯ä¸ä¸ªå¸¦æè¿åå¼çActivityï¼è¯·æ±ç 为PHOTO_REQUEST_CAREMA
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");
// è£åªæ¡çæ¯ä¾ï¼1ï¼1
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);
// å¼å¯ä¸ä¸ªå¸¦æè¿åå¼çActivityï¼è¯·æ±ç 为PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
/*
* å¤æsdcardæ¯å¦è¢«æè½½
*/
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, "æªæ¾å°åå¨å¡ï¼æ æ³åå¨ç
§çï¼", 0).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);
}
}