To open the camera after 7.0, you need to add a provider in the manifest file,
Not much to say, directly upload the code, copy it and use it directly
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fhx.imagecommit"> <uses-feature android:name="android.hardware.camera" /> <!--相机权限--> <uses-permission android:name="android.permission.CAMERA" /> <!--写入SD卡的权限:如果你希望保存相机拍照后的照片--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--读取SD卡的权限:打开相册选取图片所必须的权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.fhx.imagecommit.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> </application> </manifest>
If the problem of open failed: EACCES (Permission denied) appears after applying for dynamic permissions , then in
Add android:requestLegacyExternalStorage="true" to application
android:authorities="包名.fileprovider"
main activity
import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.io.File; public class MainActivity extends AppCompatActivity { private static int REQUEST_CAMERA = 1; private static int IMAGE_REQUEST_CODE = 2; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.image); applyWritePermission(); } public void onClick(View view) { switch (view.getId()) { case R.id.camera: useCamera(); break; case R.id.photo: getPhoto(); break; } } private File file; private void useCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/" + System.currentTimeMillis() + ".jpg"); file.getParentFile().mkdirs(); //改变Uri com.fhx.imagecommit.fileprovider注意和xml中的一致 Uri uri = FileProvider.getUriForFile(this, "com.fhx.imagecommit.fileprovider", file); //添加权限 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, REQUEST_CAMERA); } private void getPhoto() { //在这里跳转到手机系统相册里面 Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, IMAGE_REQUEST_CODE); } /** * 初始化相机相关权限 * 适配6.0+手机的运行时权限 */ public void applyWritePermission() { String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}; if (Build.VERSION.SDK_INT >= 23) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, permissions, 1); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { useCamera(); } else { // 没有获取 到权限,从新请求,或者关闭app Toast.makeText(this, "需要存储权限", Toast.LENGTH_SHORT).show(); } } private String paths; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) { Log.e("TAG", "拍照---------" + FileProvider.getUriForFile(this, "com.fhx.imagecommit.fileprovider", file)); imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath())); //在手机相册中显示刚拍摄的图片 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(file); mediaScanIntent.setData(contentUri); sendBroadcast(mediaScanIntent); } if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK) { Log.e("TAG", "选择---------" + FileProvider.getUriForFile(this, "com.fhx.imagecommit.fileprovider", file)); try { Uri selectedImage = data.getData(); //获取系统返回的照片的Uri String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片 cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); paths = cursor.getString(columnIndex); //获取照片路径 cursor.close(); Bitmap bitmap = BitmapFactory.decodeFile(paths); imageView.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:id="@+id/image" android:layout_width="150dp" android:layout_height="200dp" /> <Button android:id="@+id/camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="打开相机" /> <Button android:id="@+id/photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="打开相册" /> </LinearLayout>
Create a new xml folder Create a new filepaths.xml
My emulator 7.1
Mobile 10.0
both work perfectly,
------------------newly joined-------------------------
Android 10 If there is a problem with refreshing pictures to the picture library, if there is no refreshing, add the following method
public class UpdatePhotoAlbumUtil { /** * compatible with android 10 * Update gallery * @param mContext * @param file */ public static void updatePhotoAlbum(Context mContext, File file) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName()); values.put(MediaStore.MediaColumns.MIME_TYPE, getMimeType(file)); values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM); ContentResolver contentResolver = mContext.getContentResolver(); Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); if (uri == null) { return; } try { OutputStream out = contentResolver.openOutputStream(uri); FileInputStream fis = new FileInputStream(file); FileUtils.copy(fis, out); fis.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } else { MediaScannerConnection.scanFile(mContext.getApplicationContext(), new String[]{file.getAbsolutePath()}, new String[]{"image/jpeg"}, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { } }); } } public static String getMimeType(File file) { FileNameMap fileNameMap = URLConnection.getFileNameMap(); String type = fileNameMap.getContentTypeFor(file.getName()); return type; } }