save-media.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view v-if="imagePath !== ''" class="media-box image">
  6. <image class="image" mode="widthFix" :src="imagePath" />
  7. </view>
  8. <button type="primary" class="uni-button" @click="saveImage">拍摄图片并保存到本地</button>
  9. <view v-if="videoPath !== ''" class="media-box">
  10. <video
  11. id="myVideo"
  12. :src="videoPath"
  13. @error="videoErrorCallback"
  14. enable-danmu
  15. danmu-btn
  16. controls
  17. ></video>
  18. </view>
  19. <button type="primary" class="uni-button" @click="saveVideo">录制视频并保存到本地</button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. title: 'saveImage/saveVideo',
  28. imagePath: '',
  29. videoPath: ''
  30. };
  31. },
  32. onLoad() {},
  33. methods: {
  34. videoErrorCallback: function() {
  35. uni.showModal({
  36. content: '视频加载失败',
  37. showCancel: false
  38. });
  39. },
  40. saveImage() {
  41. uni.chooseImage({
  42. count: 1,
  43. sourceType: ['camera'],
  44. success: res => {
  45. this.imagePath = res.tempFilePaths[0];
  46. this.getTempFilePath(res.tempFilePaths[0], 'imageTempPath');
  47. },
  48. fail: (err) => {
  49. // #ifdef MP
  50. uni.getSetting({
  51. success: (res) => {
  52. let authStatus = res.authSetting['scope.camera'];
  53. if (!authStatus) {
  54. uni.showModal({
  55. title: '授权失败',
  56. content: 'Hello uni-app需要从您的相机获取图片,请在设置界面打开相关权限',
  57. success: (res) => {
  58. if (res.confirm) {
  59. uni.openSetting()
  60. }
  61. }
  62. })
  63. }
  64. }
  65. })
  66. // #endif
  67. }
  68. });
  69. },
  70. saveVideo() {
  71. let _this = this;
  72. uni.chooseVideo({
  73. count: 1,
  74. sourceType: ['camera'],
  75. success: res => {
  76. console.log(res.tempFilePath)
  77. this.videoPath = res.tempFilePath;
  78. this.getTempFilePath(res.tempFilePath, 'videoTempPath');
  79. },
  80. fail: (err) => {
  81. // #ifdef MP
  82. uni.getSetting({
  83. success: (res) => {
  84. let authStatus = res.authSetting['scope.camera'];
  85. if (!authStatus) {
  86. uni.showModal({
  87. title: '授权失败',
  88. content: 'Hello uni-app需要从您的相机获取视频,请在设置界面打开相关权限',
  89. success: (res) => {
  90. if (res.confirm) {
  91. uni.openSetting()
  92. }
  93. }
  94. })
  95. }
  96. }
  97. })
  98. // #endif
  99. }
  100. });
  101. },
  102. getTempFilePath(url, types) {
  103. // 如果已经下载本地路径,那么直接储存
  104. let obj = {
  105. filePath: url,
  106. success: () => {
  107. console.log('save success');
  108. uni.showModal({
  109. content: '保存成功',
  110. showCancel: false
  111. });
  112. uni.hideLoading();
  113. },
  114. fail: e => {
  115. uni.hideLoading();
  116. uni.showModal({
  117. content: '保存失败',
  118. showCancel: false
  119. });
  120. }
  121. };
  122. uni.showLoading({
  123. title: '保存中...'
  124. });
  125. if (types === 'videoTempPath') {
  126. uni.saveVideoToPhotosAlbum(obj);
  127. } else {
  128. uni.saveImageToPhotosAlbum(obj);
  129. }
  130. }
  131. }
  132. };
  133. </script>
  134. <style>
  135. .media-box {
  136. display: flex;
  137. justify-content: center;
  138. align-items: center;
  139. margin: 30rpx 0;
  140. width: 100%;
  141. }
  142. .image {
  143. height: 400rpx;
  144. overflow: hidden;
  145. }
  146. .image image {
  147. width: 100%;
  148. height: 100%;
  149. }
  150. video {
  151. width: 100%;
  152. }
  153. .uni-button {
  154. margin: 30rpx 0;
  155. }
  156. </style>