Saya perlu memasukkan maksud ke aplikasi kamera default untuk membuatnya mengambil foto, menyimpannya, dan mengembalikan URI. Apakah ada cara untuk melakukan ini?
android
android-camera
android-camera-intent
Alexander Oleynikov
sumber
sumber
Jawaban:
private static final int TAKE_PICTURE = 1; private Uri imageUri; public void takePhoto(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); imageUri = Uri.fromFile(photo); startActivityForResult(intent, TAKE_PICTURE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PICTURE: if (resultCode == Activity.RESULT_OK) { Uri selectedImage = imageUri; getContentResolver().notifyChange(selectedImage, null); ImageView imageView = (ImageView) findViewById(R.id.ImageView); ContentResolver cr = getContentResolver(); Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(cr, selectedImage); imageView.setImageBitmap(bitmap); Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) .show(); Log.e("Camera", e.toString()); } } } }
sumber
FileProvider
di Android> M. Lihat di siniCoba yang berikut yang saya temukan di sini
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent, 0); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode == 0) { String result = data.toURI(); // ... } }
sumber
Butuh beberapa jam untuk membuat ini berfungsi. Kode itu hampir merupakan copy-paste dari developer.android.com , dengan sedikit perbedaan.
Minta izin ini di
AndroidManifest.xml
:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Di Anda
Activity
, mulailah dengan mendefinisikan ini:static final int REQUEST_IMAGE_CAPTURE = 1; private Bitmap mImageBitmap; private String mCurrentPhotoPath; private ImageView mImageView;
Kemudian tembak ini
Intent
dionClick
:Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File Log.i(TAG, "IOException"); } // Continue only if the File was successfully created if (photoFile != null) { cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE); } }
Tambahkan metode dukungan berikut:
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 = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, // prefix ".jpg", // suffix storageDir // directory ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; }
Kemudian terima hasilnya:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { try { mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)); mImageView.setImageBitmap(mImageBitmap); } catch (IOException e) { e.printStackTrace(); } } }
Yang membuatnya berfungsi adalah
MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath))
, yang berbeda dari kode dari developer.android.com . Kode asli memberi sayaFileNotFoundException
.sumber
Coba yang berikut ini saya temukan Berikut tautannya
Jika aplikasi Anda menargetkan M dan di atasnya dan mendeklarasikan menggunakan izin CAMERA yang tidak diberikan, mencoba menggunakan tindakan ini akan menghasilkan SecurityException.
EasyImage.openCamera(Activity activity, int type);
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() { @Override public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) { //Some error handling } @Override public void onImagesPicked(List<File> imagesFiles, EasyImage.ImageSource source, int type) { //Handle the images onPhotosReturned(imagesFiles); } }); }
sumber
Saya menemukan cara yang cukup sederhana untuk melakukan ini. Gunakan tombol untuk membukanya menggunakan
on click
listener untuk memulai fungsinyaopenc()
, seperti ini:String fileloc; private void openc() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File f = null; try { f = File.createTempFile("temppic",".jpg",getApplicationContext().getCacheDir()); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,FileProvider.getUriForFile(profile.this, BuildConfig.APPLICATION_ID+".provider",f)); fileloc = Uri.fromFile(f)+""; Log.d("texts", "openc: "+fileloc); startActivityForResult(takePictureIntent, 3); } } catch (IOException e) { e.printStackTrace(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 3 && resultCode == RESULT_OK) { Log.d("texts", "onActivityResult: "+fileloc); // fileloc is the uri of the file so do whatever with it } }
Anda dapat melakukan apa pun yang Anda inginkan dengan
uri
string lokasi. Misalnya, saya mengirimkannya ke pemangkas gambar untuk memotong gambar.sumber
coba kode ini
Intent photo= new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(photo, CAMERA_PIC_REQUEST);
sumber