团队博客
拍照与相册上传。
权限添加:
<!--拍照--> <uses-permission android:name="android.permission.CAMERA" /> <!--读写--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.PERMISSIONS_STORAGE"/>
配置清单文件:
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.mooc.uploadfile4.fileprovider" android:exported="false" android:grantUriPermissions="true" tools:ignore="MissingClass"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
函数:
import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.FileProvider; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Button cameraBt; private Button photoBt; private ImageView camereIv; private ImageView photoIv; private String TAG = "tag"; //需要的权限数组 读/写/相机 private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //跳转相机动态权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); } initView(); } private Uri ImageUri; public static final int TAKE_PHOTO = 101; public static final int TAKE_CAMARA = 100; private void initView() { cameraBt = (Button) findViewById(R.id.camera_bt); photoBt = (Button) findViewById(R.id.photo_bt); camereIv = (ImageView) findViewById(R.id.camere_iv); photoIv = (ImageView) findViewById(R.id.photo_iv); cameraBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //检查是否已经获得相机的权限 if (verifyPermissions(MainActivity.this, PERMISSIONS_STORAGE[2]) == 0) { Log.i(TAG, "提示是否要授权"); ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, 3); } else { //已经有权限 toCamera(); //打开相机 } } }); photoBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toPicture(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PHOTO: if (resultCode == RESULT_OK) { try { //将拍摄的照片显示出来 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(ImageUri)); camereIv.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; case TAKE_CAMARA: if (resultCode == RESULT_OK) { try { //将相册的照片显示出来 Uri uri_photo = data.getData(); Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri_photo)); photoIv.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; default: break; } } /** * 检查是否有对应权限 * * @param activity 上下文 * @param permission 要检查的权限 * @return 结果标识 */ public int verifyPermissions(Activity activity, java.lang.String permission) { int Permission = ActivityCompat.checkSelfPermission(activity, permission); if (Permission == PackageManager.PERMISSION_GRANTED) { Log.i(TAG, "已经同意权限"); return 1; } else { Log.i(TAG, "没有同意权限"); return 0; } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (grantResults != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.i(TAG, "用户授权"); toCamera(); } else { Log.i(TAG, "用户未授权"); } } //跳转相册 private void toPicture() { Intent intent = new Intent(Intent.ACTION_PICK); //跳转到 ACTION_IMAGE_CAPTURE intent.setType("image/*"); startActivityForResult(intent, TAKE_CAMARA); Log.i(TAG, "跳转相册成功"); } //跳转相机 private void toCamera() { //创建File对象,用于存储拍照后的图片 // File outputImage = new File(getExternalCacheDir(), "outputImage.jpg"); File outputImage = new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg"); if (outputImage.exists()) { outputImage.delete(); } else { try { outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //判断SDK版本高低,ImageUri方法不同 if (Build.VERSION.SDK_INT >= 24) { ImageUri = FileProvider.getUriForFile(MainActivity.this, "com.mooc.uploadfile4.fileprovider", outputImage); } else { ImageUri = Uri.fromFile(outputImage); } //启动相机程序 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, ImageUri); startActivityForResult(intent, TAKE_PHOTO); } }
参考了些博客,目前可以获取照片但不能多选。