1. 程式人生 > >Android中通過Intent 呼叫圖片、視訊、音訊、錄音、拍照

Android中通過Intent 呼叫圖片、視訊、音訊、錄音、拍照

1. android中的一個隱藏的類:ContentType,其中定義了android支援的mimetype型別   類的路徑: com.google.android.mms.ContentType
2. Action為Intent.ACTION_GET_CONTENT的Intent可以設定許多引數,如:   intent.putExtra("crop", "true");
        intent.putExtra("outputX", appsWallpaperWidth);
        intent.putExtra("outputY", apps_wallpaper_height);
        intent.putExtra("aspectX", appsWallpaperWidth);
        intent.putExtra("aspectY", apps_wallpaper_height);
        intent.putExtra("scale", true);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("output", appsWallpaperPath);
        intent.putExtra("outputFormat", "JPEG"); 3. 使用startActivityForResult方法啟動Intent對應的Activity,可以在原Activity的回撥方法onActivityResult方法中拿到返回的資料.返回的資料放在第三個引數data(Intnet型別)中.   使用getData返回一個Uri,再使用context.getContentResolver().openInputStream(uri);可以拿到一個輸入流. //選擇圖片 requestCode 返回的標識 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT" intent.setType(contentType); //檢視型別 String IMAGE_UNSPECIFIED = "image/*"; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //新增音訊 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*"; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //拍攝視訊 int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60); Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit); intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit); startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO); //視訊 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*"; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); //錄音 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr"; intent.setClassName("com.android.soundrecorder", "com.android.soundrecorder.SoundRecorder"); ((Activity) context).startActivityForResult(intent, requestCode); //拍照 REQUEST_CODE_TAKE_PICTURE 為返回的標識 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE"; intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content://mms/scrapSpace"); startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE); 參考: