赞
踩
目录
将http//本地存放照片数据库电脑ip:端口号/fileaddress.png转image
单张照片可以使用安卓原生自带的,两张或多张照片可使用照片选择器框架【GridimageAdapter】
- /**
- * 将后端传输的数据转换为Bitmap格式
- */
- public Bitmap returnBitMap(String url) {
- URL myFileUrl = null;
- Bitmap bitmap = null;
- try {
- myFileUrl = new URL(url);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- try {
- HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return bitmap;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
imageView.setImageBitmap(getNetVideoBitmap(实体(路径)));
- //控件显示视频文件显示
-
- imageView.setImageBitmap(getNetVideoBitmap(file.getFileAddress()));
可以使用安卓原生的视频选择器
- /**
- * 视频和照片的content格式转file
- * 照片使用的是新创建照片选择器,不是安卓本身自带的,获取到的路径就是带后缀的可以直接上传
- * 视频选择是使用的安卓自带的视频选择器,获取的是content开头的文件,需要转换成file格式再上传
- * 这里的imageUri只是一个定义,不是只针对image
- */
- public static String getImageAbsolutePath(Context context, Uri imageUri) {
- if (context == null || imageUri == null)
- return null;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {
- if (isExternalStorageDocument(imageUri)) {
- String docId = DocumentsContract.getDocumentId(imageUri);
- String[] split = docId.split(":");
- String type = split[0];
- if ("primary".equalsIgnoreCase(type)) {
- return Environment.getExternalStorageDirectory() + "/" + split[1];
- }
- } else if (isDownloadsDocument(imageUri)) {
- String id = DocumentsContract.getDocumentId(imageUri);
- Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
- return getDataColumn(context, contentUri, null, null);
- } else if (isMediaDocument(imageUri)) {
- String docId = DocumentsContract.getDocumentId(imageUri);
- //定于标识
- String[] split = docId.split(":");
- String type = split[0];
- Uri contentUri = null;
- //图片执行
- if ("image".equals(type)) {
- contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- }
- //视频执行
- else if ("video".equals(type)) {
- contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
- }
- //音频执行
- else if ("audio".equals(type)) {
- contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
- }
- String selection = MediaStore.Images.Media._ID + "=?";
- String[] selectionArgs = new String[]{split[1]};
- return getDataColumn(context, contentUri, selection, selectionArgs);
- }
- } // MediaStore (and general)
- else if ("content".equalsIgnoreCase(imageUri.getScheme())) {
- // Return the remote address
- if (isGooglePhotosUri(imageUri))
- return imageUri.getLastPathSegment();
- return getDataColumn(context, imageUri, null, null);
- }
- // File
- else if ("file".equalsIgnoreCase(imageUri.getScheme())) {
- return imageUri.getPath();
- }
- return null;
- }
-
- /**
- * 从本地设备数据库查询数据.
- *
- * @param context 上下文
- * @param uri 内容提供者的标识
- * @param selection 设置条件,相当于SQL语句中的where
- * @param selectionArgs 条件值
- * @return 查询结果
- */
- public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
- Cursor cursor = null;
- String column = MediaStore.Images.Media.DATA;
- String[] projection = {column}; //告诉Provider要返回的内容(列Column)
- try {
- cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
- if (cursor != null && cursor.moveToFirst()) {
- int index = cursor.getColumnIndexOrThrow(column);
- return cursor.getString(index);
- }
- } finally {
- if (cursor != null)
- cursor.close();
- }
- return null;
- }
-
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is ExternalStorageProvider.
- */
- public static boolean isExternalStorageDocument(Uri uri) {
- return "com.android.externalstorage.documents".equals(uri.getAuthority());
- }
-
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is DownloadsProvider.
- */
- public static boolean isDownloadsDocument(Uri uri) {
- return "com.android.providers.downloads.documents".equals(uri.getAuthority());
- }
-
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is MediaProvider.
- */
- public static boolean isMediaDocument(Uri uri) {
- return "com.android.providers.media.documents".equals(uri.getAuthority());
- }
-
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is Google Photos.
- */
- public static boolean isGooglePhotosUri(Uri uri) {
- return "com.google.android.apps.photos.content".equals(uri.getAuthority());
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。