当前位置:   article > 正文

Android选择本地视频和照片上传到服务器_android上传视频

android上传视频

目录

 照片photo:

将http//本地存放照片数据库电脑ip:端口号/fileaddress.png转image

 (一)后台发送来的数据转换Bitmap的方法:

 用法:

 (二)将第一针显示出来方法:

用法:

 视频vedio:

使用选择器获取的 content文件转file文件方法


 照片photo:

单张照片可以使用安卓原生自带的,两张或多张照片可使用照片选择器框架【GridimageAdapter】

将http//本地存放照片数据库电脑ip:端口号/fileaddress.png转image

 (一)后台发送来的数据转换Bitmap的方法:

  1. /**
  2.  * 将后端传输的数据转换为Bitmap格式
  3.  */
  4. public Bitmap returnBitMap(String url) {
  5.     URL myFileUrl = null;
  6.     Bitmap bitmap = null;
  7.     try {
  8.         myFileUrl = new URL(url);
  9.     } catch (MalformedURLException e) {
  10.         e.printStackTrace();
  11.     }
  12.     try {
  13.         HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
  14.         conn.setDoInput(true);
  15.         conn.connect();
  16.         InputStream is = conn.getInputStream();
  17.         bitmap = BitmapFactory.decodeStream(is);
  18.         is.close();
  19.     } catch (IOException e) {
  20.         e.printStackTrace();
  21.     }
  22.     return bitmap;
  23. }

 用法:

imageView.setImageBitmap(getNetVideoBitmap(实体(路径)));

 (二)将第一针显示出来方法:

 

用法:

  1. //控件显示视频文件显示
  2. imageView.setImageBitmap(getNetVideoBitmap(file.getFileAddress()));

 

 视频vedio:

可以使用安卓原生的视频选择器

使用选择器获取的 content文件转file文件方法

  1. /**
  2. * 视频和照片的content格式转file
  3. * 照片使用的是新创建照片选择器,不是安卓本身自带的,获取到的路径就是带后缀的可以直接上传
  4. * 视频选择是使用的安卓自带的视频选择器,获取的是content开头的文件,需要转换成file格式再上传
  5. * 这里的imageUri只是一个定义,不是只针对image
  6. */
  7. public static String getImageAbsolutePath(Context context, Uri imageUri) {
  8. if (context == null || imageUri == null)
  9. return null;
  10. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {
  11. if (isExternalStorageDocument(imageUri)) {
  12. String docId = DocumentsContract.getDocumentId(imageUri);
  13. String[] split = docId.split(":");
  14. String type = split[0];
  15. if ("primary".equalsIgnoreCase(type)) {
  16. return Environment.getExternalStorageDirectory() + "/" + split[1];
  17. }
  18. } else if (isDownloadsDocument(imageUri)) {
  19. String id = DocumentsContract.getDocumentId(imageUri);
  20. Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
  21. return getDataColumn(context, contentUri, null, null);
  22. } else if (isMediaDocument(imageUri)) {
  23. String docId = DocumentsContract.getDocumentId(imageUri);
  24. //定于标识
  25. String[] split = docId.split(":");
  26. String type = split[0];
  27. Uri contentUri = null;
  28. //图片执行
  29. if ("image".equals(type)) {
  30. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  31. }
  32. //视频执行
  33. else if ("video".equals(type)) {
  34. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  35. }
  36. //音频执行
  37. else if ("audio".equals(type)) {
  38. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  39. }
  40. String selection = MediaStore.Images.Media._ID + "=?";
  41. String[] selectionArgs = new String[]{split[1]};
  42. return getDataColumn(context, contentUri, selection, selectionArgs);
  43. }
  44. } // MediaStore (and general)
  45. else if ("content".equalsIgnoreCase(imageUri.getScheme())) {
  46. // Return the remote address
  47. if (isGooglePhotosUri(imageUri))
  48. return imageUri.getLastPathSegment();
  49. return getDataColumn(context, imageUri, null, null);
  50. }
  51. // File
  52. else if ("file".equalsIgnoreCase(imageUri.getScheme())) {
  53. return imageUri.getPath();
  54. }
  55. return null;
  56. }
  57. /**
  58. * 从本地设备数据库查询数据.
  59. *
  60. * @param context 上下文
  61. * @param uri 内容提供者的标识
  62. * @param selection 设置条件,相当于SQL语句中的where
  63. * @param selectionArgs 条件值
  64. * @return 查询结果
  65. */
  66. public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  67. Cursor cursor = null;
  68. String column = MediaStore.Images.Media.DATA;
  69. String[] projection = {column}; //告诉Provider要返回的内容(列Column
  70. try {
  71. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
  72. if (cursor != null && cursor.moveToFirst()) {
  73. int index = cursor.getColumnIndexOrThrow(column);
  74. return cursor.getString(index);
  75. }
  76. } finally {
  77. if (cursor != null)
  78. cursor.close();
  79. }
  80. return null;
  81. }
  82. /**
  83. * @param uri The Uri to check.
  84. * @return Whether the Uri authority is ExternalStorageProvider.
  85. */
  86. public static boolean isExternalStorageDocument(Uri uri) {
  87. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  88. }
  89. /**
  90. * @param uri The Uri to check.
  91. * @return Whether the Uri authority is DownloadsProvider.
  92. */
  93. public static boolean isDownloadsDocument(Uri uri) {
  94. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  95. }
  96. /**
  97. * @param uri The Uri to check.
  98. * @return Whether the Uri authority is MediaProvider.
  99. */
  100. public static boolean isMediaDocument(Uri uri) {
  101. return "com.android.providers.media.documents".equals(uri.getAuthority());
  102. }
  103. /**
  104. * @param uri The Uri to check.
  105. * @return Whether the Uri authority is Google Photos.
  106. */
  107. public static boolean isGooglePhotosUri(Uri uri) {
  108. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  109. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/530019
推荐阅读
相关标签
  

闽ICP备14008679号