您好,登錄后才能下訂單哦!
public class MainActivity extends Activity
{
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊(cè)中選擇
private static final int PHOTO_REQUEST_CUT = 3;// 結(jié)果
private ImageView iv_p_w_picpath;
/* 頭像名稱 */
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_p_w_picpath = (ImageView) this.findViewById(R.id.iv_p_w_picpath);
}
/*
* 從相冊(cè)獲取
*/
public void gallery(View view)
{
// 激活系統(tǒng)圖庫(kù),選擇一張圖片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("p_w_picpath/*");
// 開啟一個(gè)帶有返回值的Activity,請(qǐng)求碼為PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
/*
* 從相機(jī)獲取
*/
public void camera(View view)
{
// 激活相機(jī)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判斷存儲(chǔ)卡是否可以用,可用進(jìn)行存儲(chǔ)
if (hasSdcard())
{
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
// 從文件中創(chuàng)建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// 開啟一個(gè)帶有返回值的Activity,請(qǐng)求碼為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, "p_w_picpath/*");
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);// 取消人臉識(shí)別
intent.putExtra("return-data", true);
// 開啟一個(gè)帶有返回值的Activity,請(qǐng)求碼為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)
{
// 從相冊(cè)返回的數(shù)據(jù)
if (data != null)
{
// 得到圖片的全路徑
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAREMA)
{
// 從相機(jī)返回的數(shù)據(jù)
if (hasSdcard())
{
crop(Uri.fromFile(tempFile));
} else
{
Toast.makeText(MainActivity.this, "未找到存儲(chǔ)卡,無(wú)法存儲(chǔ)照片!", 0).show();
}
} else if (requestCode == PHOTO_REQUEST_CUT)
{
// 從剪切圖片返回的數(shù)據(jù)
if (data != null)
{
Bitmap bitmap = data.getParcelableExtra("data");
this.iv_p_w_picpath.setImageBitmap(bitmap);
}
try
{
// 將臨時(shí)文件刪除
tempFile.delete();
} catch (Exception e)
{
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。