Android開啟相機和相簿執行個體代碼_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了Android開啟相機和相簿具體代碼,供大家參考,具體內容如下

開啟相機 

 /**   * 選擇相機   */  private void showCamera() {    // 跳轉到系統照相機    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    if (cameraIntent.resolveActivity(getPackageManager()) != null) {      // 設定系統相機拍照後的輸出路徑      // 建立臨時檔案      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();    }  } 

開啟相簿

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:        //請求相機        if (mTmpFile != null) {          Log.d(TAG, "onActivityResult: 請求相機: " + 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:相簿 " + 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:數量 " + 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);  }  /**   * 擷取拍照相片隱藏檔   * @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;  }}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.