iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android如何实现拍照及图片裁剪
  • 604
分享到

Android如何实现拍照及图片裁剪

android6.0 2023-05-30 19:05:35 604人浏览 泡泡鱼
摘要

这篇文章主要介绍Android如何实现拍照及图片裁剪,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!最近做项目中涉及到了图片相关功能 ,在使用安卓6.0手机及7.1手机拍照时,遇到了因权限及文件管理导致程序崩溃等问题。

这篇文章主要介绍Android如何实现拍照及图片裁剪,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

最近做项目中涉及到了图片相关功能 ,在使用安卓6.0手机及7.1手机拍照时,遇到了因权限及文件管理导致程序崩溃等问题。
 刚好把功能修改完,把代码简单地贴一下,方便以后使用。

—-主界面 代码 ——

public class MainActivity extends AppCompatActivity {  //拍照按钮  private Button take_photo;  //显示裁剪后的图片  private ImageView photo_iv;  private static final int PERMISSioNS_FOR_TAKE_PHOTO = 10;  //图片文件路径  private String picPath;  //图片对应Uri  private Uri photoUri;  //拍照对应RequestCode  public static final int SELECT_PIC_BY_TACK_PHOTO = 1;  //裁剪图片  private static final int CROP_PICTURE = 3;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    take_photo = (Button) findViewById(R.id.take_photo);    photo_iv = (ImageView) findViewById(R.id.photo_iv);    take_photo.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        //小于6.0版本直接操作        if (Build.VERSION.SDK_INT < 23) {          takePictures();        } else {          //6.0以后权限处理          permissionFORM();        }      }    });  }  @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == Activity.RESULT_OK) {      if (requestCode == SELECT_PIC_BY_TACK_PHOTO) {        String[] pojo = {MediaStore.Images.Media.DATA};        Cursor cursor = managedQuery(photoUri, pojo, null, null, null);        if (cursor != null) {          int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);          cursor.moveToFirst();          picPath = cursor.getString(columnIndex);          if (Build.VERSION.SDK_INT < 14) {            cursor.close();          }        }        if (picPath != null && (picPath.endsWith(".png") || picPath.endsWith(".PNG") || picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {          photoUri = Uri.fromFile(new File(picPath));          if (Build.VERSION.SDK_INT > 23) {            photoUri = FileProvider.getUriForFile(this, "com.innopro.bamboo.fileprovider", new File(picPath));            cropForN(picPath, CROP_PICTURE);          } else {            startPhotoZoom(photoUri, CROP_PICTURE);          }        } else {          //错误提示        }      }      if (requestCode == CROP_PICTURE) {        if (photoUri != null) {          Bitmap bitmap = BitmapFactory.decodeFile(picPath);          if (bitmap != null) {            photo_iv.setImageBitmap(bitmap);          }        }      }    }  }    private void takePictures() {    //执行拍照前,应该先判断SD卡是否存在    String SDState = Environment.getExternalStorageState();    if (SDState.equals(Environment.MEDIA_MOUNTED)) {      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);      ContentValues values = new ContentValues();      photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);      intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);      startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);    } else {      Toast.makeText(this, "手机未插入内存卡", Toast.LENGTH_LONG).show();    }  }    private void startPhotoZoom(Uri uri,                int REQUE_CODE_CROP) {    int dp = 500;    Intent intent = new Intent("com.android.camera.action.CROP");    intent.setDataAndType(uri, "image  private void cropForN(String imagePath, int REQUE_CODE_CROP) {    Uri cropUri = getImageContentUri(new File(imagePath));    Intent intent = new Intent("com.android.camera.action.CROP");    intent.setDataAndType(cropUri, "image  private void permissionForM() {    if (ContextCompat.checkSelfPermission(this,        Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,        Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {      ActivityCompat.requestPermissions(this,          new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},          PERMISSIONS_FOR_TAKE_PHOTO);    } else {      takePictures();    }  }  @Override  public void onRequestPermissionsResult(int requestCode,                      @NonNull String[] permissions, @NonNull int[] grantResults) {    if (requestCode == PERMISSIONS_FOR_TAKE_PHOTO) {      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {        takePictures();      }      return;    }    super.onRequestPermissionsResult(requestCode, permissions, grantResults);  }}

–主界面布局——–

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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="com.innopro.improve.MainActivity">  <Button    android:id="@+id/take_photo"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="拍照"    android:textSize="18sp"    app:layout_constraintLeft_toLeftOf="parent"    app:layout_constraintRight_toRightOf="parent"    app:layout_constraintTop_toTopOf="parent" />  <ImageView    android:id="@+id/photo_iv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@mipmap/ic_launcher"    app:layout_constraintLeft_toLeftOf="parent"    app:layout_constraintRight_toRightOf="parent"    app:layout_constraintTop_toBottomOf="@id/take_photo" /></android.support.constraint.ConstraintLayout>

–AndroidManifest.xml添加provider——–

   <provider      android:name="android.support.v4.content.FileProvider"      android:authorities="com.innopro.improve.fileprovider"      android:exported="false"      android:grantUriPermissions="true">      <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/file_paths" />    </provider>

–资源文件下添加xml文件夹及file_paths文件——–

<?xml version="1.0" encoding="utf-8"?><resources>  <paths>    <external-path      name="camera_photos"      path="" />  </paths></resources>

以上是“Android如何实现拍照及图片裁剪”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!

--结束END--

本文标题: Android如何实现拍照及图片裁剪

本文链接: https://www.lsjlt.com/news/220619.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c#文本框只读属性怎么设置
    c# 文本框只读属性的设置 问题:如何设置 C# 文本框的只读属性? 回答: 要设置文本框的只读属性,可以使用 ReadOnly 属性。 详细解释: ReadOnly 属性是一个布尔值属...
    99+
    2024-05-14
    c#
  • 如何使用 Golang ORM 工具与数据库交互?
    使用 gorm orm 工具与数据库交互,可通过以下步骤轻松实现:安装和初始化(1)、定义模型(2)、建立映射(3)、创建记录(4)、读取记录(5)、更新记录(6)、删除记录(7)、事务...
    99+
    2024-05-14
    golang orm mysql git iphone
  • c++中double与float的区别
    c++++ 中 double 与 float 的区别 在 C++ 中,double 和 float 都是浮点数类型,但它们在精度、范围和内存占用方面存在差异。 精度: double:双...
    99+
    2024-05-14
    c++ 内存占用
  • 如何在 Golang 中处理数据库错误?
    在 go 中处理数据库错误的步骤包括:使用专门的 go mysql 驱动程序。实现 error 接口以创建自定义错误。检测错误,记录足够的信息,并基于错误类型执行适当的恢复操作。 如何...
    99+
    2024-05-14
    golang 数据库错误 mysql git 数据丢失
  • c++中int怎么转string
    在 c++ 中将 int 转换为 string 的方法有:使用 to_string() 函数直接转换。使用 stringstream 类。使用 sprintf() 函数。 如何在 C+...
    99+
    2024-05-14
    c++
  • 优化 C++ 服务器架构以提高吞吐量
    优化 c++++ 服务器吞吐量策略:线程池:预先创建线程池,快速响应请求。非阻塞 i/o:在等待 i/o 时执行其他任务,提升吞吐量。http/2:使用二进制协议,支持多路复用和内容压缩...
    99+
    2024-05-14
    优化 服务器架构 c++
  • 使用 C++ 堆分配器管理服务器架构中的内存
    使用 c++++ 堆分配器管理服务器内存可提高性能和稳定性。堆分配器负责分配和释放动态内存,跟踪空闲/已分配内存元数据。在服务器架构中,它用于分配应用程序对象、缓冲区和数据结构。选择堆分...
    99+
    2024-05-14
    c++ 内存管理 并发访问
  • c#怎么获取字符串中的数字
    从 c# 字符串中提取数字的方法有五种:正则表达式、循环和 char.isdigit()、int.tryparse()、string.split() 和 int.parse()、linq...
    99+
    2024-05-14
    git c#
  • C++ 异常处理在服务器架构中的最佳实践
    c++++ 异常处理在服务器架构的最佳实践:定义清晰的异常层次结构,使用自定义异常类型封装相关信息。使用异常安全函数,及时在适当范围内处理异常。提供有意义的错误消息,帮助用户了解错误并采...
    99+
    2024-05-14
    c++ 异常处理
  • c#怎么拼接字符串
    在 c# 中拼接字符串有三种方法:使用加法(+)运算符、string.concat() 方法和 stringbuilder 类。最简单的方法是使用 + 运算符将字符串连接起来,...
    99+
    2024-05-14
    c#
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作