常用到的手机功能调用,录音操作等,获得视频缩略图,图片的剪裁



//调用本地相机

     Intent camare = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(camare, 2);
      break;
 
      // 调用本地相册
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_PICK);

      intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
      startActivityForResult(intent, 1);
      break;
     case 2:// 位置

      break;
     case 3:// 文件
      Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
      innerIntent.setType("*/*");
      Intent wrapperIntent = Intent.createChooser(innerIntent, null);
      startActivityForResult(wrapperIntent, 3);
      break;
     case 4:
      // 添加视频
      Intent innerVideo = new Intent(Intent.ACTION_GET_CONTENT);
      innerVideo.setType("video/*");
      Intent wrapperVideo = Intent.createChooser(innerVideo, null);
      startActivityForResult(wrapperVideo, 4);

/**
  * 给予客户端震动提示
  */
 protected void readyOperation() {
  /** 计算当前录音时长 */
  long computationTime = -1L;
  computationTime = -1L;
  Toast mRecordTipsToast = null;
  playTone(ToneGenerator.TONE_PROP_BEEP, 200);
  new Handler().postDelayed(new Runnable() {

   @Override
   public void run() {
    stopTone();
   }
  }, 200);
  /** 按键振动时长 */
  // public static final int TONE_LENGTH_MS = 200;
  vibrate(50L);
 }

 /** 开始录音 */
 public MediaRecorder startVoice() {
  String PATH = "/sdcard/MyVoiceForder/Record/";
  // 设置录音保存路径
  mFileName = PATH + UUID.randomUUID().toString() + ".amr";
  String state = android.os.Environment.getExternalStorageState();
  if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
  }
  File directory = new File(mFileName).getParentFile();
  if (!directory.exists() && !directory.mkdirs()) {
  }
  mMediaRecorder = new MediaRecorder();
  mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  mMediaRecorder.setOutputFile(mFileName);
  mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  try {
   mMediaRecorder.prepare();
  } catch (IOException e) {
  }
  mMediaRecorder.start();
  return mMediaRecorder;
 }

 /** 停止录音 */
 public void stopVoice() {
  mMediaRecorder.stop();
  mMediaRecorder.release();
  mMediaRecorder = null;
 }

// 图的剪裁
 private void startPhotoZoom(Uri uri, int size) {
  Intent intent = new Intent("com.android.camera.action.CROP");
  intent.setDataAndType(uri, "image/*");
  // crop为true是设置在开启的intent中设置显示的view可以剪裁
  intent.putExtra("crop", "true");

  // aspectX aspectY 是宽高的比例
  intent.putExtra("aspectX", 1);
  intent.putExtra("aspectY", 1);

  // outputX,outputY 是剪裁图片的宽高
  intent.putExtra("outputX", size);
  intent.putExtra("outputY", size);
  intent.putExtra("return-data", true);

  startActivityForResult(intent, BaoliaoActivity.PHOTO_REQUEST_CUT);

 }

/**
  * 本地图片转换为bitmap对象
  */
 public Bitmap convertToBitmap(String path, int w, int h) {
  BitmapFactory.Options opts = new BitmapFactory.Options();
  // 设置为ture只获取图片大小
  opts.inJustDecodeBounds = true;
  opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
  // 返回为空
  BitmapFactory.decodeFile(path, opts);
  int width = opts.outWidth;
  int height = opts.outHeight;
  float scaleWidth = 0.f, scaleHeight = 0.f;
  if (width > w || height > h) {
   // 缩放
   scaleWidth = ((float) width) / w;
   scaleHeight = ((float) height) / h;
  }
  opts.inJustDecodeBounds = false;
  float scale = Math.max(scaleWidth, scaleHeight);
  opts.inSampleSize = (int) scale;
  WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
  return Bitmap.createScaledBitmap(weak.get(), w, h, true);
 }

 /**
  * 获取视频的缩略图 先通过ThumbnailUtils来创建一个视频的缩略图
  */
 private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
  Bitmap bitmap = null;
  // 获取视频的缩略图
  bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
  System.out.println("w" + bitmap.getWidth());
  System.out.println("h" + bitmap.getHeight());
  bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
  return bitmap;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值