https://developer.android.com/guide/topics/media/camera.html
但是onactivityresult返回的 intent是空的.
因为使用了.EXTRA_OUTPUT.这个的作用是.当生成图片时 会默认保存已经指定的文件里
下面是调用 camera的方法。可以看到使用了 EXTRA_OUTPUT
private Uri fileUri;/** * 카메라 열기 */public void openCamera(){ Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name startActivityForResult(intent,CAMERA_CAPTURE);}
下面是官方给的 生成图片文件的方法
/** Create a file Uri for saving an image or video */private static Uri getOutputMediaFileUri(int type){ return Uri.fromFile(getOutputMediaFile(type));} /** Create a File for saving an image or video */private static File getOutputMediaFile(int type){ // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "MyCameraApp"); // This location works best if you want the created images to be shared // between applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ MakeContent.getInstance().ContentErrorLogger("failed to create directory"); return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE){ mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"); } else if(type == MEDIA_TYPE_VIDEO) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4"); } else { return null; } return mediaFile;}
下面是在 onActivityResult 里返回文件。这里需要注意的是 有的机型 返回Intent为空.
所以直接拿过来 fileUri 即可
if (requestCode == CAMERA_CAPTURE) { if (resultCode == RESULT_OK) { // Image captured and saved to fileUri specified in the Intent gallaryFilePath( fileUri.toString().replaceFirst("file://","")); } else if (resultCode == RESULT_CANCELED) { // User cancelled the image capture } else { // Image capture failed, advise user } }
没有评论:
发表评论