官网文档
拍照功能点击拍照后可以进行拍照,并且将得到的照片展现在屏幕上
添加相机权限
JAVA代码
package Picture; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.media.Image; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import com.example.myapp_b.R; public class MainActivity extends AppCompatActivity { ImageView imageView; static final int REQUEST_IMAGE_CAPTURE=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); imageView=findViewById(R.id.iv); } public void takePic(View view) { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if(intent.resolveActivity(getPackageManager())!=null){ startActivityForResult(intent,REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); imageView.setImageBitmap(imageBitmap); } } }
XML代码
保存到本地
package Picture; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; import android.content.Intent; import android.graphics.Bitmap; import android.media.Image; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import com.example.myapp_b.R; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { ImageView imageView; static final int REQUEST_IMAGE_CAPTURE=1; static final int RESULT_OK=-1; String path=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); imageView=findViewById(R.id.iv); } public void takePic(View view) throws IOException { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启系统相机的意图对象 String imagePath= Environment.getExternalStorageDirectory()+"/DCIM";//设置照片保存路径 File file=new File(imagePath, "default_image.jpg"); //File file=createImageFile(); path=file.getAbsolutePath(); //使用文件提供者获取路径URL Uri contentUri= FileProvider.getUriForFile(this,"com.example.android.fileprovider",file); //设置传递参数 让相机拍照后保存照片 intent.putExtra(MediaStore.EXTRA_OUTPUT,contentUri); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { //1.设置扫描意图 Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); //2.设置路径 File f=new File(path); //3.获取路径 Uri contenturi=Uri.fromFile(f);//file:///storage/emulated/0/DCIM/default_image.jpg mediaScanIntent.setData(contenturi); System.out.println(contenturi.toString()); //4.发送广播 this.sendBroadcast(mediaScanIntent); // Bundle extras = data.getExtras(); // Bitmap imageBitmap = (Bitmap) extras.get("data"); // imageView.setImageBitmap(imageBitmap); } } private File createImageFile() throws IOException { // 创建独一无二的文件名 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, ".jpg", storageDir ); // Save a file: path for use with ACTION_VIEW intents path = image.getAbsolutePath(); return image; } }
配置文件
file_path.xml
不重复文件名
上述方法得到的文件名是重复的,参考官网文档可以将其的名字加上时间
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File file=new File(imagePath, "JPEG_" + timeStamp + ".jpg");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)