智慧屏安装APP的最佳实践与跨平台小程序开发的结合
676
2022-11-17
【安卓开发】调用手机的相机进行拍照并且保存在本地的APP
官网文档
拍照功能
添加相机权限
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-.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, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // 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");
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~