-
2021-05-26 13:41:15
该功能使用到的场景比较多,下面能过一个实例介绍其使用,布局比较简单只有两个控件,上面Button下面ImageView,Button用于打开摄像头进行拍照,而ImageView用于将拍到的图片显示出来。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56public class CameraTestActivityextends AppCompatActivity {
public static final int TAKE_PHOTO =1;
private ImageView picture;
private Uri imageUri;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_test);
Button takePhoto = (Button)findViewById(R.id.take_photo);
picture = (ImageView)findViewById(R.id.picture);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File outputImage =new File(getExternalCacheDir(),"output_image.jpg");
try{
if (outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >=24){//android7.0及以上
imageUri = FileProvider.getUriForFile(CameraTestActivity.this,
"com.example.cameratest.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);
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
switch (requestCode){
case TAKE_PHOTO:
if (resultCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
首先创建一个File对象,用于存放摄像头拍下的图片,这里将图片命名为output_image.jpg,并将它存放在手机SD卡的应用关联缓存目录下。什么叫做应用关联缓存目录呢?就是指SD卡中专门用于存放当前应用缓存数据的位置,调用getExternalCacheDir()方法可以得到这个目录,具体的路径是/sdcard/Android/data/(package name)/cache。那么为什么要使用应用关联缓存目录来存放图片呢?因为从Android6.0系统开始,读写SD卡被列为危险权限,如里将图片存放在SD卡的任何其它目录,都要进行运行时权限处理才行,而使用应用关联目录则可以跳过这一步。
接着进行一个判断,如果运行设备的系统版本低于Android7.0,就调用Uri的fromFile()方法将File对象转换成Uri对象,这个Uri对象标识着output_image.jpg这张图片的本地真实路径。否则,就调用FileProvider的getUriForFile()方法将File对象转换成一个封装过的Uri对象。之所以要进行这样一层转换,是因为从Android7.0系统开始,直接使用本地真实路径的Uri被认为是不安全的,会抛出一个FileUriExposedException异常。而FileProvider是一种特殊的内容提供器,它使用了和内容提供器类似的机制来对数据进行保护,可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性。
因为使用到了内容提供器FileProvider,自然需要在AndroidManifest.xml中进行注册。
1
2
3
4
5...
...
其中,android:name属性的值是固定的,android:authorities属性的值必须要和刚才FileProvider.getUriForFile()方法中的第二个参数一致。另外,这里还在标签内部使用来指定Uri的共享路径,并引用了一个@xml/file_paths资源。这个资源的内容如下:
1
2
3
4
其中,external-path就是用来指定Uri共享的,name属性的值可以随便填,path属性的值表示共享的具体路径。这里设置空值就表示将整个SD卡进行共享,当然也可仅共享我们存放output_image.jpg这张图片的路径。
另外还有一点要注意,在Android4.4系统之前,访问SD卡的应用关联目录也是要声明权限的,从4.4系统开始不再需要权限声明。那么我们为了兼容老版本系统的手机,还需要在清单文件中声明一下访问SD卡的权限:
更多相关内容 -
Android调用摄像头拍照
2022-04-17 22:07:36Android调用摄像头拍照这篇来学习一下如何调用摄像头拍照。
学习自《第一行代码》
以下内容皆为真机测试。
xml文件
就一个按钮和一个显示图片的。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btnTakePhoto" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo"/> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
MainActivity中的代码
public class MainActivity extends AppCompatActivity { private int takePhoto = 1; private Uri imageUri; private File outputImage; private Button btnTakePhoto; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnTakePhoto = findViewById(R.id.btnTakePhoto); imageView = findViewById(R.id.imageView); // 拍照按钮 btnTakePhoto.setOnClickListener(view -> { // 创建File对象,用于存储拍照后的图片,命名为output_image.jdp // 存放在手机SD卡的应用关联缓存目录下 outputImage = new File(getExternalCacheDir(), "output_image.jpg"); if (outputImage.exists()) { outputImage.delete(); } try { outputImage.createNewFile(); // 如果运行设备的系统高于Android 7.0 // 就调用FileProvider的getUriForFile()方法将File对象转换成一个封装过的Uri对象。 // 该方法接收3个参数:Context对象, 任意唯一的字符串, 创建的File对象。 // 这样做的原因:Android 7.0 开始,直接使用本地真实路径的Uri是被认为是不安全的,会抛出FileUriExposedException异常; // 而FileProvider是一种特殊的ContentProvider,他使用了和ContentProvider类似的机制对数据进行保护,可以选择性地将封装过的Uri共享给外部。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { imageUri = FileProvider.getUriForFile(this, "com.example.permissiontest.fileprovider", outputImage); } else { // 否则,就调用Uri的fromFile()方法将File对象转换成Uri对象 imageUri = Uri.fromFile(outputImage); } // 启动相机 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); // 指定图片的输出地址,这样拍下的照片会被输出到output_image.jpg中。 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, takePhoto); } catch (IOException e) { e.printStackTrace(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == takePhoto) { if (resultCode == Activity.RESULT_OK) { // 将拍摄的照片显示出来 try { // decodeStream()可以将output_image.jpg解析成Bitmap对象。 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); imageView.setImageBitmap(rotateIfRequired(bitmap)); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } /** * 为了防止横屏照片时,返回产生的旋转,所以这里做的是将照片始终正常的竖屏显示 * @param bitmap * @return */ private Bitmap rotateIfRequired(Bitmap bitmap) { try { ExifInterface exifInterface = new ExifInterface(outputImage.getPath()); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) return rotateBitmap(bitmap, 90); else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) return rotateBitmap(bitmap, 180); else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) return rotateBitmap(bitmap, 270); return bitmap; } catch (IOException e) { e.printStackTrace(); } return null; } private Bitmap rotateBitmap(Bitmap bitmap, int degree) { Matrix matrix = new Matrix(); matrix.postRotate((float)degree); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // 将不再需要的Bitmap对象回收 bitmap.recycle(); return rotatedBitmap; } }
注册ContentProvider
上述方法中用到了ContentProvider,所以需要去AndroidManifest.xml对它进行注册:
<application ... <provider android:authorities="com.example.permissiontest.fileprovider" android:name="androidx.core.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider> <activity ... </activity> </application>
其中android:name属性的值是固定的,android:authorities属性的值需要和刚才FileProvider.getUriForFile()方法的第二个参数相同。
这里还使用了
<meta-data>
标签指定Uri的共享路径,并引用了@xml/file_paths资源。现在需要去创建这个资源。在res目录下新建文件夹xml,然后新建一个File,命名为file_paths.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="/"/> </paths>
其中, external-path是用来指定Uri共享路径的,name属性的值随便填,path值表示共享的具体路径。单斜杠表示将整个SD卡进行共享。
大功告成,现在去真机上测试时没有问题。
-
Android调用摄像头拍照(兼容7.0)
2018-04-08 11:26:57Android调用摄像头拍照(兼容7.0)Demo,原博客文章https://blog.csdn.net/u010356768/article/details/70808162 -
Android调用摄像头拍照开发教程
2020-08-27 15:31:35主要为大家详细介绍了Android调用摄像头拍照的开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android实现调用摄像头进行拍照功能
2020-08-27 15:32:04主要为大家详细介绍了Android实现调用摄像头进行拍照功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android实现调用摄像头拍照与视频功能
2020-08-27 15:32:11主要为大家详细介绍了Android实现调用摄像头拍照与视频功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
android 调用摄像头拍照并显示相片
2019-03-08 10:34:22适配所有的安卓版本 调用手机摄像头 实现拍照 保存相片 -
Android中实现调用摄像头拍照并显示在ImageView中示例代码.zip
2020-12-01 20:30:14Android中实现调用摄像头拍照并显示在ImageView中示例代码 -
android调用摄像头实时预览
2017-06-19 10:33:34是在320*320屏幕,mdpi上的调用摄像头预览 -
Android实现调用摄像头和相册的方法
2020-08-27 15:14:07主要为大家详细介绍了Android实现调用摄像头和相册的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android调用系统摄像头拍照并显示在ImageView上
2020-08-27 15:13:46主要为大家详细介绍了Android调用系统摄像头拍照并显示在ImageView上,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Android开发之调用摄像头拍照(Android 第一行代码)
2022-04-04 18:27:21Android开发之调用摄像头拍照(Android 第一行代码)布局文件(拍照按钮+图片显示)
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照"/> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity { public static final int TAKE_PHOTO=1; private ImageView picture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button takePhoto=findViewById(R.id.take_photo); picture=findViewById(R.id.picture); takePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //创建File对象,用来存储拍照后的照片 //getExternalCacheDir()获取此应用缓存数据的位置,在这个位置保存图片 File outputImage=new File(getExternalCacheDir(),"output_image.jpg"); try{ if (outputImage.exists()){//如果图片已经存在就删除再重新创建 outputImage.delete(); } outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } if (Build.VERSION.SDK_INT>=24){ imageUri= FileProvider.getUriForFile(MainActivity.this, "com.example.camcreaablumtest.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); } }); } @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)); picture.setImageBitmap(bitmap); }catch (FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } } }
Mainfest(这里有一行代码和书中不一样,书上的会标红)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.camreaalbumtest"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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:authorities="com.example.camreaalbumtest.fileprovider" android:name="androidx.core.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider> </application> </manifest>
android:name="android.support.v4.content.FileProvider" 改为 android:name="androidx.core.content.FileProvider"
新建路径文件
在res目录下新建文件夹xml,在xml文件夹中新建flie_paths.xml文件,编辑内容如下
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="/"/> </paths>
结束
-
Android调用摄像头拍照/从相册中选择照片
2021-06-30 14:21:45编写activity_main.xml ...RelativeLayout 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" an编写activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" /> <Button android:id="@+id/map_depot" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/take_photo" android:text="图库" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/map_depot" android:layout_gravity="center_horizontal" /> </RelativeLayout>
编写MainActivity
package com.axs.photo; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import android.Manifest; import android.annotation.TargetApi; import android.content.ContentUris; 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.provider.DocumentsContract; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity { private static final int TAKE_PHOTO = 1; private ImageView picture; private Uri imageUri; public static final int CHOOSE_PHOTO = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //拍照按钮 Button takePhoto = (Button) findViewById(R.id.take_photo); picture = (ImageView) findViewById(R.id.picture); takePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 创建File对象,用于存储拍照后的图片 // 将图片命名为output_image.jpg,并将它存放在手机SD卡的应用关联缓存目录下 // getExternalCacheDir()可以得到这个目录 File outputImage = new File(getExternalCacheDir(),"output_image.jpg"); try { if (outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } // 进行一个判断 // 如果运行设备的系统版本低于Android 7.0,就调用Uri的fromFile()方法将File对象转换成Uri对象 // 这个Uri对象标识着output_image.jpg这张图片的本地真实路径 // 否则,就调用FileProvider的getUriForFile()方法将File对象转换成一个封装过的Uri对象 // getUriForFile()方法接收 3个参数,第一个参数要求传入Context对象,第二个参数可以是任意唯一的字符串,第三个参数则是我们刚刚创建的File对象 // 之所以要进行这样一层转换,是因为从Android 7.0系统开始,直接使用本地真实路径的Uri被认为是不安全的,会抛出一个FileUriExposedException 异常 // 而FileProvider则是-种特殊的内容提供器,它使用了和内容提供器类似的机制来对数据进行保护,可以选择性地将封装过的Uri共享给外部,从而提高了应用的安全性 if (Build.VERSION.SDK_INT >= 24){ imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.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); } }); //照片按钮 Button chooseFromAlbum = (Button) findViewById(R.id.map_depot); chooseFromAlbum.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //动态申请读取SD卡权限 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE},1); }else{ openAlbum(); } } }); } //报错 Overriding method should call super.onActivityResult //不影响结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ switch (requestCode){ case TAKE_PHOTO: if (resultCode == RESULT_OK){ try { //将拍摄的照片显示出来 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); picture.setImageBitmap(bitmap); }catch (FileNotFoundException e){ e.printStackTrace(); } } break; case CHOOSE_PHOTO: if (resultCode == RESULT_OK){ //判断手机系统版本号 if (Build.VERSION.SDK_INT >= 19){ //4.4及以上系统使用这个方法处理图片 handleImageOnKitKat(data); }else{ handleImageBeforeKitKat(data); } } break; default: break; } } private void openAlbum(){ Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent,CHOOSE_PHOTO); //打开相册 } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ switch (requestCode){ case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ openAlbum(); }else{ Toast.makeText(this,"You denied the permission", Toast.LENGTH_SHORT).show(); } break; default: } } @TargetApi(19) private void handleImageOnKitKat(Intent data){ String imagePath = null; Uri uri = data.getData(); if(DocumentsContract.isDocumentUri(this,uri)){ /*如果是document类型的Uri,则通过document id处理*/ String docId = DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id = docId.split(":")[1]; //解析出数字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId)); imagePath = getImagePath(contentUri,null); } }else if("content".equalsIgnoreCase(uri.getScheme())){ //如果是content类型的uri,则使用普通方式处理 imagePath = getImagePath(uri,null); }else if("file".equalsIgnoreCase(uri.getScheme())){ //如果是file类型的uri,直接获取图片路径即可 imagePath = uri.getPath(); } displayImage(imagePath); //根据图片路径显示图片 } private void handleImageBeforeKitKat(Intent data){ Uri uri = data.getData(); String imagePath = getImagePath(uri,null); displayImage(imagePath); } private String getImagePath(Uri uri,String selection){ String path = null; //通过Uri和selection来获取真实的图片路径 Cursor cursor = getContentResolver().query(uri,null,selection,null,null); if(cursor != null){ if (cursor.moveToFirst()){ path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; } private void displayImage(String imagePath){ if(imagePath != null){ Bitmap bitmap = BitmapFactory.decodeFile(imagePath); picture.setImageBitmap(bitmap); }else { Toast.makeText(this,"failed to get image", Toast.LENGTH_SHORT).show(); } } }
在res目录下新建文件夹xml
在xml下进行操作New→File
命名为file_paths.xml<?xml version ="1.0" encoding ="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path=""><!--path为空时表示将整个SD卡进行共享,这里会报错,不影响结果--> </external-path> </paths>
编写AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.axs.photo"> <!--声明访问SD卡的权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Photo"> <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.example.cameraalbumtest.fileprovider" android:exported="false" android:grantUriPermissions="true" > <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application> </manifest>
接着就可以拍照或者访问图库了
如果访问图库后图片没有回显的报错:open failed: EACCES (Permission denied)
用的Android 10.0及以上的Android设备,请动态申请权限,并在 AndroidManifest.xml中application标签内加上android:requestLegacyExternalStorage=“true”
造成原因:自Android10.0起,为了能够更好的使用分区存储,为此,请确保针对搭载 Android 10(API 级别 29)及更高版本的设备强制启用了该行为。<application ... android:requestLegacyExternalStorage="true" ... > ... </application>
-
Android实现调用摄像头
2020-09-03 13:01:05本文给大家分享的是,在安卓APP开发的过程中,经常会需要调用手机自身摄像头拍照的代码,十分的简单实用,有需要的小伙伴可以参考下。 -
Android读取本地图库与调用摄像头拍摄
2021-01-04 16:44:50本文主要介绍如何读取Android本地图库的图片以及调用安卓的摄像头进行拍摄。 一、布局 布局比较简单,MainActviivty的布局文件只有两个按钮,一个是读取图库的,另一个是打开摄像头的,另外ResultActivity的布局只有... -
C# Xamarin.Android WebView Vue调用手机摄像头
2022-04-20 18:17:20C# Xamarin.Android ...Input调用手机摄像头上传图片,或通过选择器选中拍照还是图片 Js交互传递Location坐标信息 动态申请权限 开发版本:Visual2019,Visual2022编译通过 注意: AndroidManifest.xml配置文件 -
HTML5调用android手机摄像头拍照
2021-05-26 17:38:17但实际上用html5调用手机摄像头存在很多问题:1)谷歌的发布的Chrome到了21版本后,才新增了一个用于高质量视频音频通讯的getUserMedia API,该API允许Web应用程序访问摄像头和麦克风,其他手机浏览器只有opera支持... -
Android 多媒体应用——调用摄像头
2022-05-17 19:02:41点击 Button 调用摄像头进行拍照 将拍的照片显示在界面中 接下来,我们开始实战吧 Demo实现 1. 设置布局 新建一个项目,名为:CameraApplication 在 activity_main.xml 中设置布局,布局中设置 -
Android调用前后摄像头同时工作实例代码
2020-08-30 01:41:03本篇文章主要介绍了Android调用前后摄像头同时工作实例代码,这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。 -
Android开发教程之调用摄像头功能的方法详解
2021-01-05 10:43:27本文实例讲述了Android调用摄像头功能的方法。分享给大家供大家参考,具体如下: 我们要调用摄像头的拍照功能,显然 第一步必须加入调用摄像头硬件的权限,拍完照后我们要将图片保存在SD卡中,必须加入SD卡读写权限... -
仿QQAndroid调用摄像头拍照和从相册中选择(上传头像)
2017-05-23 21:43:21仿QQAndroid调用摄像头拍照和从相册中选择(上传、更换头像) -
Android【摄像头的调用和照片的存储】
2022-01-11 10:10:39Android调用摄像头并显示拍照后的图片到ImageView中 1、前期准备 需要在Manifest中添加相关权限 <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.... -
Android 调用摄像头功能
2014-05-05 14:35:43在Android开发过程中,有时需要调用手机自身设备的功能,本演示项目侧重摄像头拍照功能的调用。 -
Android 调用摄像头和相册
2021-12-04 16:13:19调用摄像头 很多应用都会要求用户上传图片,这时打开摄像头拍照是最简单快捷的 新建一个项目,修改 activity_main.xml 中的代码,如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:... -
使用Androidstudio调用摄像头拍照并保存照片
2021-05-29 12:20:48首先在manifest.xmlns文件中声明权限 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ... android:requestLegacyExternalStorage="true -
Android 调用摄像头功能【拍照与视频】Demo
2014-06-03 20:52:01在Android开发过程中,有时需要调用手机自身设备的功能,上个案例主要侧重摄像头拍照功能的调用。本例将综合实现拍照与视频的操作。