中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android編程實現調用相冊、相機及拍照后直接裁剪的方法

發布時間:2020-08-23 19:44:52 來源:腳本之家 閱讀:600 作者:Jacob-wj 欄目:移動開發

本文實例講述了Android編程實現調用相冊、相機及拍照后直接裁剪的方法。分享給大家供大家參考,具體如下:

package com.cvte.health.phone;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import com.cvte.health.AccountManager;
import com.cvte.health.HealthApplication;
import com.cvte.health.R;
import com.cvte.health.api.ImageUpdater;
import com.cvte.health.database.User;
public class ChangePortraitActivity extends Activity {
  private ImageView mImageView;
  private Button mButtonCamera;
  private Button mButtonPhoto;
  private Button mButtonBack;
  private Button mButtonSave;
  private User mUser = null;
  private File mCurrentPhotoFile;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_portrait);
    HealthApplication.getInstance().addActivity(this);
    mUser = AccountManager.getInstance().getCurrentUser();
    mImageView = (ImageView) this.findViewById(R.id.imageview_preview);
    mButtonCamera = (Button) this.findViewById(R.id.button_cameraButton);
    mButtonPhoto = (Button) this.findViewById(R.id.button_photoButton);
    mButtonBack = (Button) findViewById(R.id.button_back);
    mButtonSave = (Button) findViewById(R.id.button_save);
    ImageUpdater.getInstance(this).updateUserPhoto(mUser, mImageView);
    mButtonCamera.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        mCurrentPhotoFile = new File("mnt/sdcard/DCIM/Camera/",
            getPhotoFileName());
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(mCurrentPhotoFile));
        startActivityForResult(intent, Activity.DEFAULT_KEYS_DIALER);
        /*
         * Intent intent = new
         * Intent("android.media.action.IMAGE_CAPTURE");
         * intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
         * File(Environment .getExternalStorageDirectory(),
         * "camera.jpg")));
         * intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
         * startActivityForResult(intent, 10);
         */
      }
    });
    mButtonPhoto.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 11);
      }
    });
    mButtonBack.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();
      }
    });
    mButtonSave.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(ChangePortraitActivity.this,
            UserProfileActivity.class);
        mImageView.setDrawingCacheEnabled(Boolean.TRUE);
        intent.putExtra("BITMAP", mImageView.getDrawingCache()); // 這里可以放一個bitmap
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.activity_in_from_left,
            R.anim.activity_out_from_right);
      }
    });
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
      mImageView.setImageDrawable(Drawable.createFromPath(new File(
          Environment.getExternalStorageDirectory(), "camera.jpg")
          .getAbsolutePath()));
    } else if (requestCode == 11 && resultCode == Activity.RESULT_OK) {
      Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
      mImageView.setImageBitmap(cameraBitmap);
    } else if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
      Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      ContentResolver cr = ChangePortraitActivity.this
          .getContentResolver();
      Uri fileUri = Uri.fromFile(mCurrentPhotoFile);
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
          fileUri));
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      Cursor cursor = cr.query(imgUri, null,
          MediaStore.Images.Media.DISPLAY_NAME + "='"
              + mCurrentPhotoFile.getName() + "'", null, null);
      Uri uri = null;
      if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToLast();
        long id = cursor.getLong(0);
        uri = ContentUris.withAppendedId(imgUri, id);
      }
      final Intent intent = new Intent("com.android.camera.action.CROP");
      intent.setDataAndType(uri, "image/*");
      intent.putExtra("crop", "true");
      intent.putExtra("aspectX", 1);
      intent.putExtra("aspectY", 1);
      intent.putExtra("outputX", 300);
      intent.putExtra("outputY", 300);
      intent.putExtra("return-data", true);
      ChangePortraitActivity.this.startActivityForResult(intent, 3);
    } else if (requestCode == 3 && resultCode == Activity.RESULT_OK) {
      Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
      mImageView.setImageBitmap(cameraBitmap);
    }
  }
  private String getPhotoFileName() {
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat dateFormat = new SimpleDateFormat(
        "'IMG'_yyyyMMdd_HHmmss");
    return dateFormat.format(date) + ".jpg";
  }
}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android拍照與圖片處理技巧總結》、《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

邵武市| 鄂伦春自治旗| 荥经县| 临清市| 昌邑市| 信丰县| 青海省| 南召县| 三明市| 东至县| 扬州市| 石河子市| 城固县| 二连浩特市| 嘉峪关市| 芷江| 清水河县| 泸定县| 白水县| 安新县| 河东区| 黑龙江省| 滦南县| 德令哈市| 拉萨市| 祁门县| 林芝县| 蒙阴县| 山丹县| 邓州市| 海原县| 夏邑县| 曲靖市| 清流县| 米脂县| 读书| 漳平市| 若尔盖县| 南和县| 牙克石市| 象山县|