安卓开发之图片分享应用4:发布图片功能的实现
一、效果演示
结合前面的登录注册功能,以及底部导航功能,实现点击底部导航的发布按钮之后,进入该功能,可以对图片进行上传,预览,编辑。
二、下载第三方开源九宫格图片展示器库(带编辑功能)
1. Github网址
经过几天的寻找,终于发现了一个对我本人来说感觉不错的九宫格图片展示器库。
Github
网址为:https://github.com/LwkCoder/NineGridView
2. 将该库克隆(或者下载)到本地
①使用Git
克隆到本地
使用Git
克隆的话可以使用下方命令clone
(克隆)到本地:
git clone https://github.com/LwkCoder/NineGridView.git
报错:OpenSSL SSL_read: Connection was reset, errno 10054
当然因为是Github
,所以网络可能不好,克隆会失败,所以可以多试几次
②使用下载方式的话直接下,下载结束后解压即可。
三、将下载好的项目(九宫格图片库)作为library导入到我们的项目中使用
1. 修改整个项目的公共文件
可以同时打开两个项目,我们先将项目的结构设为Project
模式
然后直接将九宫格图片库的library
文件夹(依赖)复制到我们的项目PictureAPP
中,因为我将名称改为mylibrary
,所以这里显示的是mylibrary
我们复制进来的library
依赖的文件夹图标与正常的依赖图片不一样,正常的依赖图标是这样子的:
然而我们显示的图标是这样子的:
说明我们的依赖还没有添加成功,我们先将缺少的文件复制到我们的项目中:
报错没关系,先不动,我们打开项目的build.gradle
,注意是项目的,而不是app
里面的
build.gradle
内容如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config-mavencentral.gradle"
buildscript {
repositories {
mavenCentral()
google()
maven { url "https://maven.aliyun.com/repository/google"}
maven { url "https://maven.aliyun.com/repository/jcenter"}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
然后往gradle.properties
里面添加下面内容
gradle.properties
里添加的内容如下
MIN_SDK_VERSION=14
COMPILE_SDK_VERSION=30
TARGET_SDK_VERSION=30
APPLICATION_ID=com.lwkandroid.ngvsample
VERSION_CODE=1
VERSION_NAME=NineGridViewSample
GlideVersion=4.12.0
ImagePickerVersion=1.5.0
AppCompatXVersion = 1.3.0
ConstraintLayoutVersion=2.0.4
android.enableJetifier=true
修改settings.gradle
完整代码如下
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url "https://maven.aliyun.com/repository/google"}
maven {
url "https://maven.aliyun.com/repository/jcenter"}
}
}
rootProject.name = "PictureApp"
include ':app' , ':mylibrary'
2. 修改library
我们将library
打开,这里我们·先修改一下包名,因为会存在包名冲突问题
打开library后,点击AndroidManifest.xml文件
可以先跳过修改包名这一步,等后面显示包名冲突再回来改,不然可能会出现其他问题。
修改一下包名即可,我只是在原来的包名后面加了个x
,改成其他也可以
最后一步,修改一下library
里面的build.gradle
,那么library
就修改完成了
build.gradle:
apply plugin: 'com.android.library'
apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"
android {
compileSdkVersion COMPILE_SDK_VERSION as int
defaultConfig {
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.appcompat:appcompat:1.3.0"
}
allprojects {
tasks.withType(Javadoc) {
//兼容中文字符
options {
encoding "UTF-8"
charSet 'UTF-8'
links "http://docs.oracle.com/javase/7/docs/api"
}
}
}
3. 修改app文件
先将九宫格图片库里面的这两个文件复制到app
文件夹里,有一个是原来就存在的,直接覆盖(overWrite)
修改app
的build.gradle
添加依赖
完整代码如下
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.pictureapp"
minSdk 28
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard