本文實(shí)例為大家分享了Android打開(kāi)相機(jī)和相冊(cè)具體代碼,供大家參考,具體內(nèi)容如下
打開(kāi)相機(jī)
/** * 選擇相機(jī) */ private void showCamera() { // 跳轉(zhuǎn)到系統(tǒng)照相機(jī) Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getPackageManager()) != null) { // 設(shè)置系統(tǒng)相機(jī)拍照后的輸出路徑 // 創(chuàng)建臨時(shí)文件 mTmpFile = OtherUtils.createFile(getApplicationContext()); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile)); startActivityForResult(cameraIntent, REQUEST_CAMERA); } else { Toast.makeText(getApplicationContext(), R.string.msg_no_camera, Toast.LENGTH_SHORT).show(); } } 打開(kāi)相冊(cè)
Intent albumIntent = new Intent(Intent.ACTION_PICK, null); albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(albumIntent, REQUEST_ALBUM_OK);
onActivityResult
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CAMERA: //請(qǐng)求相機(jī) if (mTmpFile != null) { Log.d(TAG, "onActivityResult: 請(qǐng)求相機(jī): " + mTmpFile.getAbsolutePath()); Picasso.with(this).load(mTmpFile).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100)) .error(R.mipmap.pictures_no).into(mIvDispaly); } break; case REQUEST_ALBUM_OK: Log.d(TAG, "onActivityResult:相冊(cè) " + data.getData().toString()); ContentResolver resolver = getContentResolver(); try { InputStream inputStream = resolver.openInputStream(data.getData()); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mIvDispaly.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } // Cursor query = resolver.query(data.getData(), null, null, null, null);//// String str = null;// while (query.moveToNext()) {// Log.d(TAG, "onActivityResult:數(shù)量 " + query.getCount());// str =query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));// Log.d(TAG, "onActivityResult: 列名" + query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA)));// }// query.close();// Picasso.with(this).load(new File(str)).centerCrop().resize(OtherUtils.dip2px(this,100),OtherUtils.dip2px(this,100))// .error(R.mipmap.pictures_no).into(mIvDispaly); break; } } OtherUtils
import android.content.Context;import android.os.Environment;import java.io.File;import java.util.Date;/** * Created by 劉楠 on 2016/7/20 0020.22:37 */public class OtherUtils { public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 獲取拍照相片存儲(chǔ)文件 * @param context * @return */ public static File createFile(Context context){ File file; if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String timeStamp = String.valueOf(new Date().getTime()); file = new File(Environment.getExternalStorageDirectory() + File.separator + timeStamp+".jpg"); }else{ File cacheDir = context.getCacheDir(); String timeStamp = String.valueOf(new Date().getTime()); file = new File(cacheDir, timeStamp+".jpg"); } return file; }}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選