加入消息提醒功能,每次发消息,使用通知显示人名、头像和内容

加入头像设置功能,支持拍照选取和从相册选取,裁剪

加入播放器功能,支持播放音乐和视频

首先看一下项目的总体:

项目名为Total,它是由三部分组成:Activity.java文件,9.png图形文件和xml布局文件,他简略的将之前所做过的部分实验合成在了一起;实现了部分功能:活动之间的跳转功能,发送消息功能,发送通知功能,上传图片和拍照功能以及调用多媒体功能。

实现流程:首先我们简单设置一个主功能界面用来显示几种功能:
创建 activity_main.xml文件
代码如下:


```java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/lu">
<!--    //版权保护:NG麒麟-->
<!--    //    Zc--><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="聊天界面"/><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="发送通知"/><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="修改头像"/><Buttonandroid:id="@+id/button4"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="多媒体"/></LinearLayout>

效果图如下:

编写activity函数;
给每一个button设置监听器并且设置相对应的activity;
代码如下:(注意红线处修改)

需设置为自己的对应的按钮和类名;

package com.example.total;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;import com.example.total.Communicate.Talk;public class MainActivity extends AppCompatActivity {//版权保护:NG麒麟
//    Zc@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,Talk.class);startActivityForResult(intent, 1);}});Button button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,Notification.class);startActivityForResult(intent, 1);}});Button button3 = (Button) findViewById(R.id.button3);button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,Change.class);startActivityForResult(intent, 1);}});Button button4 = (Button) findViewById(R.id.button4);button4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this,Media.class);startActivityForResult(intent, 1);}});}
}

之后从发送通知开始依次创建其相应的xml文件和activity文件,并且在创建的同时需要在manifest中注册activity;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.total"><applicationandroid: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.Total"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".Notification"/><activity android:name=".Change"/><activity android:name=".Media"/><activity android:name=".Communicate.Talk"/></application></manifest>

1、 发送通知:
创建activity_notification.xml:设置一个简单的发送通知按钮;
代码如下:

<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"android:background="@drawable/zc_qiao"><Button android:id="@+id/send_notice"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="发送通知"/></LinearLayout>

创建Notification活动类,编写相应的函数;

主要代码如下:

package com.example.total;import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;@RequiresApi(api = Build.VERSION_CODES.O)
public class Notification extends AppCompatActivity implements View.OnClickListener{//版权保护:NG麒麟
//    ZcNotificationManager manager ;String id ="channel_1";//channel的idString description = "123";//channel的描述信息int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification);Button sendNotice = findViewById(R.id.send_notice);sendNotice.setOnClickListener(this);manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic void onClick(View view) {manager.createNotificationChannel(channel);//添加channelswitch (view.getId()){case R.id.send_notice:android.app.Notification notification = new NotificationCompat.Builder(this,id).setContentTitle("赴火给您发来一条消息").setContentText("你在干嘛呢").setWhen(System.currentTimeMillis())
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setLargeIcon(BitmapFactory.decodeearth.9.pngResource(getResources(),R.mipmap.ic_launcher)).setSmallIcon(R.drawable.cc).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.cc)).build();manager.notify(1,notification);break;default:break;}}}

注意:在Android O后 引入了一个叫NotificationChannel的类 在sdk版本为26的时候 不加入此类就设置用不了点击事件;

之后连接起来活动和布局之后就可以运行了;
效果图如下:

2、 修改头像;
同上,首先创建activity_change.xml;
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/zc_qiao"><Buttonandroid:id="@+id/fromAlbum"android:layout_width="150dp"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="从相册选取" /><Buttonandroid:id="@+id/takePhoto"android:layout_width="150dp"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="拍照" /><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp" /></LinearLayout>

(简单设置了两个按钮用来实现调用相机功能调用相册上传照片和裁剪功能)
(拍照需要连接手机,虚拟机似乎没有拍照功能,调用相册可以直接使用)

之后编写Change活动类:
代码如下:

package com.example.total;import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.io.File;public class Change extends AppCompatActivity {//版权保护:NG麒麟
//    Zcprivate Button mFromAlbum; // 从相册选取private Button mTakePhotos; // 照相private ImageView mImgView;private final int FROM_ALBUM = 0;private final int FROM_TAKE_PHOTOS = 1;private final int PHOTO_CUT = 2;private final String FILE_NAME = "tempimg.jpg";private File tempFile;private Bitmap bitmap;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_change);mImgView = (ImageView) findViewById(R.id.img);mFromAlbum = (Button) findViewById(R.id.fromAlbum);mTakePhotos = (Button) findViewById(R.id.takePhoto);//从相册选取照片mFromAlbum.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(Intent.ACTION_PICK);intent.setType("image/*");startActivityForResult(intent, FROM_ALBUM);}});//即时拍摄头像mTakePhotos.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent("android.media.action.IMAGE_CAPTURE");// 判断存储卡是否可以用,可用进行存储if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILE_NAME)));}startActivityForResult(intent, FROM_TAKE_PHOTOS);}});}@SuppressLint("WrongConstant")@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (requestCode) {case FROM_ALBUM:if (data != null) {// 取到照片路径Uri uri = data.getData();editPic(uri);}break;case FROM_TAKE_PHOTOS:if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {tempFile = new File(Environment.getExternalStorageDirectory(),FILE_NAME);editPic(Uri.fromFile(tempFile));} else {Toast.makeText(Change.this, "未找到存储卡!", 0).show();}break;case PHOTO_CUT:try {bitmap = data.getParcelableExtra("data");mImgView.setImageBitmap(bitmap);//删除缓存图片tempFile.delete();} catch (Exception e) {e.printStackTrace();}break;}super.onActivityResult(requestCode, resultCode, data);}/** 编辑选中的照片 */private void editPic(Uri uri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");// 裁剪框的比例,1:1intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// 裁剪后输出图片的尺寸大小intent.putExtra("outputX", 250);intent.putExtra("outputY", 250);intent.putExtra("outputFormat", "JPEG");intent.putExtra("noFaceDetection", true);intent.putExtra("return-data", true);startActivityForResult(intent, PHOTO_CUT);}
}

代码中设置了相对应的鼠标监听器功能;调用摄像头功能,从相册中选取照片时需判断url路径和存储路径等,以及编辑照片时的editPic功能等;
效果图如下:


最终即可得到效果图;照片上传成功;
拍照功能接入手机打开开发者模式接入USB即可运行;

3、 多媒体功能:
4、 创建activity_media.xml文件;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="3dp"android:orientation="vertical"android:background="@drawable/zc_qiao"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/play"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="播放" /><Buttonandroid:id="@+id/pause"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="暂停" /><Buttonandroid:id="@+id/stop"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="停止" /></LinearLayout><SeekBarandroid:id="@+id/seekbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="0"android:progress="0"android:secondaryProgress="0" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="当前时间" /><TextViewandroid:id="@+id/tv2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="总时间" /></RelativeLayout></LinearLayout>

编写Media类活动函数;

package com.example.total;import java.io.File;
import java.io.IOException;import android.R.integer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;public class Media extends Activity implements OnClickListener,OnSeekBarChangeListener {//版权保护:NG麒麟
//    Zcprivate Button play, pause, stop;private MediaPlayer player;private SeekBar mSeekBar;private TextView tv, tv2;private boolean hadDestroy = false;private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case 0x01:break;default:break;}};};Runnable runnable = new Runnable() {@Overridepublic void run() {if (!hadDestroy) {mHandler.postDelayed(this, 1000);int currentTime = Math.round(player.getCurrentPosition() / 1000);String currentStr = String.format("%s%02d:%02d", "当前时间 ",currentTime / 60, currentTime % 60);tv.setText(currentStr);mSeekBar.setProgress(player.getCurrentPosition());}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_media);play = (Button) findViewById(R.id.play);pause = (Button) findViewById(R.id.pause);stop = (Button) findViewById(R.id.stop);mSeekBar = (SeekBar) findViewById(R.id.seekbar);tv = (TextView) findViewById(R.id.tv);tv2 = (TextView) findViewById(R.id.tv2);mSeekBar.setOnSeekBarChangeListener(this);play.setOnClickListener(this);pause.setOnClickListener(this);stop.setOnClickListener(this);player = new MediaPlayer();initMediaplayer();}/*** 初始化播放器*/private void initMediaplayer() {try {File file = new File(Environment.getExternalStorageDirectory()+ "/Download/", "aiqiu.mp3");player.setDataSource(file.getPath());Log.e("播放器", file.toString());player.prepare();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.play:if (!player.isPlaying()) {player.start();int totalTime = Math.round(player.getDuration() / 1000);String str = String.format("%02d:%02d", totalTime / 60,totalTime % 60);tv2.setText(str);mSeekBar.setMax(player.getDuration());mHandler.postDelayed(runnable, 1000);}break;case R.id.pause:if (player.isPlaying()) {player.pause();}break;case R.id.stop:if (player.isPlaying()) {player.reset();initMediaplayer();}break;default:break;}}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {if (player != null) {player.seekTo(seekBar.getProgress());}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO 自动生成的方法存根}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO 自动生成的方法存根}@Overrideprotected void onDestroy() {// TODO 自动生成的方法存根super.onDestroy();if (player != null) {player.stop();hadDestroy = true;player.release();}}}

效果图如下:

同样需要导入手机中的音频文件才可以播放;

其中还加入了发送消息的功能正如图1所示:


布局比较简单就不放了;
Talk.activity代码:

package com.example.total.Communicate;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;import com.example.total.R;import java.util.ArrayList;
import java.util.List;public class Talk extends Activity {private List<Msg> msgList = new ArrayList<>();private EditText inputText;private Button send;private RecyclerView msgRecyclerView;private MsgAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_talk);initMsgs();//初始化消息数据inputText = (EditText) findViewById(R.id.input_text);//消息内容绑定send = (Button) findViewById(R.id.send);//按钮绑定发送msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);//recycleView 绑定图片LinearLayoutManager layoutManager = new LinearLayoutManager(this);//绑定当前类上下文msgRecyclerView.setLayoutManager(layoutManager);//将绑定好的上下文添加到recycleViewadapter = new MsgAdapter(msgList);//newmsgRecyclerView.setAdapter(adapter);//设置send.setOnClickListener(new View.OnClickListener() {     //点击事件@Overridepublic void onClick(View v) {String content = inputText.getText().toString();if (!("".equals(content))) {Toast.makeText(Talk.this,"sure",Toast.LENGTH_SHORT).show();//show msgMsg msg = new Msg(content,Msg.TYPE_SENT);msgList.add(msg);adapter.notifyItemInserted(msgList.size()-1);msgRecyclerView.scrollToPosition(msgList.size()-1);inputText.setText("");//clear}}});}private void initMsgs() {Msg msg1 = new Msg("Hello",Msg.TYPE_RECEIVED);msgList.add(msg1);Msg msg2 = new Msg("I'm John",Msg.TYPE_RECEIVED);msgList.add(msg2);Msg msg4 = new Msg("who are you",Msg.TYPE_RECEIVED);msgList.add(msg2);Msg msg3 = new Msg("Hello",Msg.TYPE_SENT);msgList.add(msg3);}
}

谢谢关注,持续更新;

Android多媒体实现拍照,调用相册,音频等功能相关推荐

  1. 兼容Android 11 相机拍照,从相册中选择,裁剪图片

    由于android 11对存储空间进行了更新,导致无法进入裁剪或者裁剪后无法保存,返回路径等问题. android 10以下可以参考:android 相机拍照,从相册中选择,裁剪图片 前面部分和之前的 ...

  2. Android拍照和相册+系统裁剪功能返回图片

    最近在使用一加3手机,Android系统6.0,进行测试的时候,发现调用手机的拍照和相册选择图片的功能返回的时候都无法调用系统的裁剪功能,Log日志也没有输出有用的信息.经过在网上大量的查找资料,拍照 ...

  3. Android 头像选择(拍照、相册裁剪),含7.0的坑

    作者:夏至,欢迎转载,但请保留这段申明,谢谢. http://blog.csdn.net/u011418943/article/details/77712662 首先,好规则,看看自己的实现效果: 当 ...

  4. Android实现相机拍照和相册选择以及图片裁剪适配Android10以上

    之前写的一个工具类,在华为手机Android版本12上无法返回图片路径,提示不是一个文件或是文件不存在. 所以更改此工具类如下: package com.suoer.comeonhealth.laib ...

  5. android全格式多媒体播放器,Android多媒体应用使用MediaPlayer播放音频

    Android提供了对常用音频和视频格式的支持,它所支持的音频格式有MP3(.mp3).3GPP(.3gp).Ogg(.ogg)和WAVE(.ave)等,支持的视频格式有3GPP(.3gp)和MPEG ...

  6. android多媒体部分学习笔记八------音频录制 mediaRecorder

    /**  * 原始音频的播放和录制  *   * audio  *   * audioTrack  *   *   * @time 下午12:58:03  * @author retacn yue   ...

  7. android实现手机拍照以及图片预览功能_手机系统将有A/B分区?Android 11这些变化你关注过吗...

    跳票让Android 11沉淀下来并吸引了更多的消费者,在iPhone SE大规模进军主流消费市场的今天,Android这边难道不想依靠新系统扳回一局吗?在人们感叹iOS一些功能似曾相似的时候,And ...

  8. android编程 自动裁剪图片,Android编程实现调用相册、相机及拍照后直接裁剪的方法...

    本文实例讲述了Android编程实现调用相册.相机及拍照后直接裁剪的方法.分享给大家供大家参考,具体如下: package com.cvte.health.phone; import java.io. ...

  9. android webview中h5调用 拍照/相册 通用支持代码

    android webview中h5调用 拍照/相册 通用支持代码. 在webview的   WebChromeClient 中,重写 // For Android >= 5.0 @Overri ...

最新文章

  1. php能实现文本复制吗,php 简单复制文件的方法
  2. ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
  3. linux下安装配置使用memcache,memcached,libevent(后有ubuntu的memcache安装)
  4. 《梦断代码》读后感一
  5. IntelliJ IDEA版本和junit版本不适配
  6. IDEA的Struts2项目报错java.lang.ClassNotFoundException
  7. Redlock 算法:Redis 实现分布式锁(译)
  8. 设置darktable为中文
  9. Android测试——在Python中如何将Windows程序前台显示
  10. Android卡片设置透明度失效问题
  11. 如何用T—SQL命令查询一个数据库中有哪些表?
  12. SQLiteDeveloper破解方法
  13. 网络安全等级保护三级产品清单整理
  14. python 苹果电脑 怎么安装_mac怎么安装python2.7(python苹果电脑如何安装教程)
  15. python输入一段英文_编写程序,用户输入一段英文,然后输出这段英文中所有长度为 3 个字母的单词。_学小易找答案...
  16. 智能晾衣器全国产化电子元件推荐方案
  17. java8:lambda级联表达式(Cascading)或柯里化(Currying)原理简化详解
  18. 高德地图获取坐标距离_高德地图获取两个经纬度点间直线距离JS/PHP/SQL代码
  19. VMware虚拟机安装macOS系统
  20. 普通用户使用su无法切到root用户的解决方法

热门文章

  1. 如何提高用户体验之某费控独角兽公司的分享-现场分享
  2. 蓝牙技术|AirPods Pro 2或将搭载运动传感器,TWS蓝牙耳机发展新方向
  3. 【Protobuf协议】003-嵌套类型、更新一个消息类型、Any、Oneof、Map(映射)、包
  4. ctfshow RCE极限挑战
  5. python银行利息问题_python要求用户输入本金,银行年利率和...
  6. 嵌入式系统设计信息交流 微信讨论组
  7. 前端笔记:动画介绍与CSS动画说明
  8. 微信小程序Ⅰ [关于微信支付的一点思路]
  9. 使用JQuery修改Table特定行的相关属性
  10. python干饭神器---今天吃什么?python告诉你