android 相册选择图片_安卓上最好的第三方相机应用

(1) 2024-09-27 09:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
android 相册选择图片_安卓上最好的第三方相机应用,希望能够帮助你!!!。

利用android takephoto v4.1.0库实现拍照,相册选择

此处直接用takephoto开源的代码库,不用依赖的方式,下载地址后续上传。

一,布局如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btnPhoto1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" /> <Button android:id="@+id/btnPhoto2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从相册获取" /> <Button android:id="@+id/btnUpdate" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" android:text="上传" /> <ImageView android:id="@+id/ivPhoto" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

二,代码如下:

package com.leansmall.testphoto; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.ContextWrapper; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.bumptech.glide.Glide; import org.devio.takephoto.app.TakePhoto; import org.devio.takephoto.app.TakePhotoActivity; import org.devio.takephoto.compress.CompressConfig; import org.devio.takephoto.model.CropOptions; import org.devio.takephoto.model.TResult; import java.io.File; import java.util.ArrayList; import java.util.List; public class MainActivity extends TakePhotoActivity { private Button btnPhoto; private Button btnPhotos; private Button btnUpdate; private ImageView ivPhoto; //动态权限 String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}; List<String> mPermissionList = new ArrayList<>(); private final int mRequestCode = 100;//权限请求码 //TakePhoto private TakePhoto takePhoto; private CropOptions cropOptions; //裁剪参数 private CompressConfig compressConfig; //压缩参数 private Uri imageUri; //图片保存路径 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= 23) { //6.0才用动态权限 //申请相关权限 initPermission(); } initData(); //设置压缩、裁剪参数 initView(); } private void initData() { // 获取TakePhoto实例 takePhoto = getTakePhoto(); //设置裁剪参数 cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(false).create(); //设置压缩参数 compressConfig = new CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create(); takePhoto.onEnableCompress(compressConfig, true); //设置为需要压缩 } private void initPermission() { mPermissionList.clear(); //清空没有通过的权限 //逐个判断你要的权限是否已经通过 for (int i = 0; i < permissions.length; i++) { if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { mPermissionList.add(permissions[i]);//添加还未授予的权限 } } //申请权限 if (mPermissionList.size() > 0) {//有权限没有通过,需要申请 ActivityCompat.requestPermissions(this, permissions, mRequestCode); } else { //说明权限都已经通过,可以做你想做的事情去 } } private void initView() { btnPhoto = findViewById(R.id.btnPhoto1); btnPhotos = findViewById(R.id.btnPhoto2); btnUpdate = findViewById(R.id.btnUpdate); ivPhoto = findViewById(R.id.ivPhoto); btnPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("", "btnPhoto"); imageUri = getImageCropUri(); //拍照并裁剪 // takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions); //仅仅拍照不裁剪 takePhoto.onPickFromCapture(imageUri); } }); btnPhotos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("", "btnPhotos"); // onClickk(getTakePhoto()); imageUri = getImageCropUri(); // //从相册中选取图片并裁剪 // takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions); // //从相册中选取不裁剪 takePhoto.onPickFromGallery(); } }); } //从本地选取的第二种方法,可设置多张选择 public void onClickk(TakePhoto takePhoto) { configCompress(takePhoto); takePhoto.onPickMultiple(3); //3张图片 // takePhoto.onPickFromGallery();//根据需求这里面放最大图片数 一张图片takePhoto.onPickFromGallery(); } private void configCompress(TakePhoto takePhoto) { //压缩配置 int maxSize = Integer.parseInt("");//最大 压缩 int width = Integer.parseInt("800");//宽 int height = Integer.parseInt("800");//高 CompressConfig config; config = new CompressConfig.Builder().setMaxSize(maxSize) .setMaxPixel(width >= height ? width : height) .enableReserveRaw(false)//拍照压缩后是否显示原图 .create(); takePhoto.onEnableCompress(config, false);//是否显示进度条 } @Override public void takeSuccess(TResult result) { super.takeSuccess(result); Log.i("", "takeSuccess : " + result.getImage().getOriginalPath()); super.takeSuccess(result); String iconPath = result.getImage().getOriginalPath(); //Toast显示图片路径 Toast.makeText(this, "imagePath:" + iconPath, Toast.LENGTH_SHORT).show(); //Google Glide库 用于加载图片资源 Glide.with(this).load(iconPath).into(ivPhoto); } @Override public void takeFail(TResult result, String msg) { super.takeFail(result, msg); Log.i("", "takeFail : " + msg); } @Override public void takeCancel() { super.takeCancel(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); boolean hasPermissionDismiss = false;//有权限没有通过 if (mRequestCode == requestCode) { for (int i = 0; i < grantResults.length; i++) { if (grantResults[i] == -1) { hasPermissionDismiss = true; } } //如果有权限没有被允许 if (hasPermissionDismiss) { // showPermissionDialog();//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问 } else { //全部权限通过,可以进行下一步操作。。。 } } } //获得照片的输出保存Uri private Uri getImageCropUri() { ContextWrapper cw = new ContextWrapper(getApplicationContext()); File directory = cw.getExternalFilesDir(Environment.DIRECTORY_PICTURES); File file = new File(directory, System.currentTimeMillis() + ".jpg"); // File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); return Uri.fromFile(file); } }

三、module gradle:

apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.leansmall.testphoto" minSdkVersion 26 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation project(path: ':takephoto_library') implementation 'com.github.bumptech.glide:glide:3.8.0' } 

四,Project gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} maven { url 'https://dl.bintray.com/umsdk/release' } google() mavenCentral() jcenter{url "http://jcenter.bintray.com/"} } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} maven { url 'https://dl.bintray.com/umsdk/release' } google() mavenCentral() jcenter{url "http://jcenter.bintray.com/"} } } task clean(type: Delete) { delete rootProject.buildDir } 

五、takephoto gradle:

apply plugin: 'com.android.library' android { compileSdkVersion 28 defaultConfig { minSdkVersion 14 targetSdkVersion 27 versionCode 45 versionName "4.1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.soundcloud.android.crop:lib_crop:1.0.0' implementation 'com.darsh.multipleimageselect:multipleimageselect:1.0.5' implementation 'me.shaohui.advancedluban:library:1.3.2' implementation 'androidx.annotation:annotation:1.1.0' } //apply from: "bintrayUpload.gradle"

六、gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

七,效果图如下:

android 相册选择图片_安卓上最好的第三方相机应用_https://bianchenghao6.com/blog__第1张

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复