public class MainActivity extends Activity
成都創(chuàng)新互聯(lián)專注于章貢網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供章貢營銷型網(wǎng)站建設(shè),章貢網(wǎng)站制作、章貢網(wǎng)頁設(shè)計(jì)、章貢網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造章貢網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供章貢網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
{
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊中選擇
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);
}
/*
* 從相冊獲取
*/
public void gallery(View view)
{
// 激活系統(tǒng)圖庫,選擇一張圖片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("p_w_picpath/*");
// 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
/*
* 從相機(jī)獲取
*/
public void camera(View view)
{
// 激活相機(jī)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判斷存儲卡是否可以用,可用進(jìn)行存儲
if (hasSdcard())
{
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
// 從文件中創(chuàng)建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, "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);// 取消人臉識別
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)
{
// 從相冊返回的數(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, "未找到存儲卡,無法存儲照片!", 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);
}
}
網(wǎng)站欄目:android如何從系統(tǒng)或相機(jī)中獲取照片
本文鏈接:http://vcdvsql.cn/article26/jhgsjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機(jī)、網(wǎng)站制作、ChatGPT、網(wǎng)站排名、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)