private static final int CAMERA_REQUEST = 1337;private void showCamera(){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra( “类别”,…
试试这个,希望它能帮到你。
if (requestCode == CAMERA_REQUEST) { Bitmap bitmap= (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(bitmap); }
要使用图像文件路径,您必须执行以下操作
的 1)编写一种方法来创建图像文件 强>
String currentPhotoPath; private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents currentPhotoPath = image.getAbsolutePath(); return image; }
的 2)调用相机意图 强>
private void showCamera() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } } }
的 3) 强> 现在,您需要配置FileProvider。在应用的清单中,向您的应用添加提供商:
<application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> </provider> ... </application>
的 4)创建资源文件 res/xml/file_paths.xml 强>
res/xml/file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /> </paths>
的 注意: 强> 确保您更换 com.example.package.name 与您的应用程序的实际包名称。
com.example.package.name
的 5)获取imageFilePath 强>
if (requestCode == CAMERA_REQUEST) { Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), currentPhotoPath); imageView.setImageBitmap(bitmap); // or you can use Glide to show image Glide.with(this).load(currentPhotoPath).into(imageView); }
希望它按预期工作。有关详细信息,您可以看到谷歌 官方文件 !
inal int RC_TAKE_PHOTO = 1; private void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(getActivity().getExternalCacheDir(), String.valueOf(System.currentTimeMillis()) + ".jpg"); fileUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); getActivity().startActivityForResult(intent, RC_TAKE_PHOTO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_TAKE_PHOTO && resultCode == RESULT_OK) { //do whatever you need with taken photo using file or fileUri } } }
我用这个代码。但是getActivity是错误的吗?我必须改变吗?