Commit 48faa6aba0f385df34bf0185a4bf97528ad7fec8
1 parent
93d57aa7
图片选取问题处理
Showing
2 changed files
with
297 additions
and
21 deletions
app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
| @@ -2,17 +2,13 @@ package com.cnlive.im.ui; | @@ -2,17 +2,13 @@ package com.cnlive.im.ui; | ||
| 2 | 2 | ||
| 3 | import android.Manifest; | 3 | import android.Manifest; |
| 4 | import android.app.Activity; | 4 | import android.app.Activity; |
| 5 | -import android.content.ContentResolver; | ||
| 6 | import android.content.Context; | 5 | import android.content.Context; |
| 7 | import android.content.Intent; | 6 | import android.content.Intent; |
| 8 | import android.content.pm.PackageManager; | 7 | import android.content.pm.PackageManager; |
| 9 | -import android.database.Cursor; | ||
| 10 | import android.net.Uri; | 8 | import android.net.Uri; |
| 11 | import android.os.Build; | 9 | import android.os.Build; |
| 12 | import android.os.Bundle; | 10 | import android.os.Bundle; |
| 13 | import android.os.Handler; | 11 | import android.os.Handler; |
| 14 | -import android.provider.ContactsContract; | ||
| 15 | -import android.provider.MediaStore; | ||
| 16 | import android.support.annotation.NonNull; | 12 | import android.support.annotation.NonNull; |
| 17 | import android.support.v4.app.ActivityCompat; | 13 | import android.support.v4.app.ActivityCompat; |
| 18 | import android.support.v4.content.ContextCompat; | 14 | import android.support.v4.content.ContextCompat; |
| @@ -42,10 +38,10 @@ import com.cnlive.im.mode.ImagMessage; | @@ -42,10 +38,10 @@ import com.cnlive.im.mode.ImagMessage; | ||
| 42 | import com.cnlive.im.mode.StringMessage; | 38 | import com.cnlive.im.mode.StringMessage; |
| 43 | import com.cnlive.im.mode.VoiceMessage; | 39 | import com.cnlive.im.mode.VoiceMessage; |
| 44 | import com.cnlive.im.util.AudioRecordUtil; | 40 | import com.cnlive.im.util.AudioRecordUtil; |
| 45 | -import com.cnlive.im.util.MediaRecorderUtil; | ||
| 46 | import com.cnlive.im.util.MessageType; | 41 | import com.cnlive.im.util.MessageType; |
| 47 | import com.cnlive.im.util.PlayerUtil; | 42 | import com.cnlive.im.util.PlayerUtil; |
| 48 | import com.cnlive.im.util.TimeUtils; | 43 | import com.cnlive.im.util.TimeUtils; |
| 44 | +import com.cnlive.im.util.UploadImageUtils; | ||
| 49 | import com.cnlive.libs.util.chat.ChatUtil; | 45 | import com.cnlive.libs.util.chat.ChatUtil; |
| 50 | import com.cnlive.libs.util.chat.base.IChat; | 46 | import com.cnlive.libs.util.chat.base.IChat; |
| 51 | import com.cnlive.libs.util.chat.model.CNImgMessage; | 47 | import com.cnlive.libs.util.chat.model.CNImgMessage; |
| @@ -55,11 +51,8 @@ import com.cnlive.libs.util.chat.model.CNVoiceMessage; | @@ -55,11 +51,8 @@ import com.cnlive.libs.util.chat.model.CNVoiceMessage; | ||
| 55 | 51 | ||
| 56 | import org.json.JSONException; | 52 | import org.json.JSONException; |
| 57 | 53 | ||
| 58 | -import java.io.LineNumberReader; | ||
| 59 | import java.util.ArrayList; | 54 | import java.util.ArrayList; |
| 60 | 55 | ||
| 61 | -import static android.R.id.edit; | ||
| 62 | - | ||
| 63 | 56 | ||
| 64 | /** | 57 | /** |
| 65 | * @author 陈硕 | 58 | * @author 陈硕 |
| @@ -396,18 +389,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -396,18 +389,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 396 | if (resultCode == Activity.RESULT_OK) { | 389 | if (resultCode == Activity.RESULT_OK) { |
| 397 | switch (requestCode) { | 390 | switch (requestCode) { |
| 398 | case IMG_FROM_PICTURE: | 391 | case IMG_FROM_PICTURE: |
| 399 | -// String localImagePath = "/sdcard/test.png"; | ||
| 400 | -// Uri selectedImage = Uri.parse("file://" + localImagePath); | ||
| 401 | Uri selectedImage = data.getData(); | 392 | Uri selectedImage = data.getData(); |
| 402 | - String[] filePathColumn = {MediaStore.Images.Media.DATA}; | ||
| 403 | - | ||
| 404 | - Cursor cursor = getContentResolver().query(selectedImage, | ||
| 405 | - filePathColumn, null, null, null); | ||
| 406 | - cursor.moveToFirst(); | ||
| 407 | - | ||
| 408 | - int columnIndex = cursor.getColumnIndex(filePathColumn[0]); | ||
| 409 | - final String picturePath = cursor.getString(columnIndex); | ||
| 410 | - | 393 | + String picturePath = UploadImageUtils.getPath(this, selectedImage); |
| 411 | Uri uri = Uri.parse("file://" + picturePath); | 394 | Uri uri = Uri.parse("file://" + picturePath); |
| 412 | ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener); | 395 | ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener); |
| 413 | break; | 396 | break; |
| @@ -599,10 +582,10 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -599,10 +582,10 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 599 | 582 | ||
| 600 | @Override | 583 | @Override |
| 601 | public void afterTextChanged(Editable editable) { | 584 | public void afterTextChanged(Editable editable) { |
| 602 | - if (!TextUtils.isEmpty(editable)){ | 585 | + if (!TextUtils.isEmpty(editable)) { |
| 603 | ima_send.setVisibility(View.GONE); | 586 | ima_send.setVisibility(View.GONE); |
| 604 | send_messageOneBtn.setVisibility(View.VISIBLE); | 587 | send_messageOneBtn.setVisibility(View.VISIBLE); |
| 605 | - }else { | 588 | + } else { |
| 606 | send_messageOneBtn.setVisibility(View.GONE); | 589 | send_messageOneBtn.setVisibility(View.GONE); |
| 607 | ima_send.setVisibility(View.VISIBLE); | 590 | ima_send.setVisibility(View.VISIBLE); |
| 608 | } | 591 | } |
app/src/main/java/com/cnlive/im/util/UploadImageUtils.java
0 → 100644
| 1 | +package com.cnlive.im.util; | ||
| 2 | + | ||
| 3 | +import android.annotation.TargetApi; | ||
| 4 | +import android.content.ContentUris; | ||
| 5 | +import android.content.Context; | ||
| 6 | +import android.database.Cursor; | ||
| 7 | +import android.graphics.Bitmap; | ||
| 8 | +import android.graphics.BitmapFactory; | ||
| 9 | +import android.graphics.Matrix; | ||
| 10 | +import android.media.ExifInterface; | ||
| 11 | +import android.net.Uri; | ||
| 12 | +import android.os.Build; | ||
| 13 | +import android.os.Environment; | ||
| 14 | +import android.provider.DocumentsContract; | ||
| 15 | +import android.provider.MediaStore; | ||
| 16 | +import android.text.TextUtils; | ||
| 17 | +import android.widget.Toast; | ||
| 18 | + | ||
| 19 | +import java.io.ByteArrayOutputStream; | ||
| 20 | +import java.io.File; | ||
| 21 | +import java.io.FileOutputStream; | ||
| 22 | +import java.io.IOException; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Created by gujun on 2015/5/19. | ||
| 26 | + */ | ||
| 27 | +public class UploadImageUtils { | ||
| 28 | + private static final String IMAGE_PATH = "/com.cnlive.shockwave/image/"; | ||
| 29 | + | ||
| 30 | + private static ByteArrayOutputStream getImageStream(String srcPath) { | ||
| 31 | +// BitmapFactory.Options newOpts = new BitmapFactory.Options(); | ||
| 32 | +// //开始读入图片,此时把options.inJustDecodeBounds 设回true了 | ||
| 33 | +// newOpts.inJustDecodeBounds = true; | ||
| 34 | +// Bitmap bitmap ; | ||
| 35 | +// BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空 | ||
| 36 | +// newOpts.inJustDecodeBounds = false; | ||
| 37 | +// int scale = 0; | ||
| 38 | +// if(newOpts.outHeight <= newOpts.outWidth){//竖图 | ||
| 39 | +// scale = (int) (newOpts.outHeight / (float) 600); | ||
| 40 | +// }else{//横图 | ||
| 41 | +// scale = (int) (newOpts.outWidth / (float) 600); | ||
| 42 | +// } | ||
| 43 | +// if (scale <= 0) | ||
| 44 | +// scale = 1; | ||
| 45 | +// newOpts.inSampleSize = scale;//设置缩放比例 | ||
| 46 | +// //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 | ||
| 47 | +// bitmap = BitmapFactory.decodeFile(srcPath, newOpts); | ||
| 48 | + Bitmap bitmap = BitmapFactory.decodeFile(srcPath); | ||
| 49 | + int oldWidth = bitmap.getWidth(); | ||
| 50 | + int oldHeight = bitmap.getHeight(); | ||
| 51 | + float scale; | ||
| 52 | + if(oldHeight <= oldWidth){ | ||
| 53 | + scale = (float)600 / oldHeight; | ||
| 54 | + }else{ | ||
| 55 | + scale = (float)600 / oldWidth; | ||
| 56 | + } | ||
| 57 | + if(scale > 1){ | ||
| 58 | + scale = 1 ; | ||
| 59 | + } | ||
| 60 | + Matrix matrix = new Matrix(); | ||
| 61 | + matrix.postScale(scale,scale); | ||
| 62 | + bitmap = Bitmap.createBitmap(bitmap,0,0,oldWidth,oldHeight,matrix,true); | ||
| 63 | + //解决部分手机相机返回照片自动旋转问题 | ||
| 64 | + bitmap = rotate(bitmap,readPictureDegree(srcPath)); | ||
| 65 | + return compressImage(bitmap);//压缩好比例大小后再进行质量压缩 | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + private static ByteArrayOutputStream compressImage(Bitmap image) { | ||
| 69 | + ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); | ||
| 70 | + image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到bitmapStream中 | ||
| 71 | + return bitmapStream; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + public static String writeStringToSDcard(Context context,String path) throws IOException { | ||
| 75 | + ByteArrayOutputStream bitmapStream = getImageStream(path); | ||
| 76 | + return writeToSDcard(context,bitmapStream); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + private static String writeToSDcard(Context context,ByteArrayOutputStream bitmapStream)throws IOException { | ||
| 80 | + String filePath = getImagePath(context); | ||
| 81 | + if(!TextUtils.isEmpty(filePath)){ | ||
| 82 | + FileOutputStream out=new FileOutputStream(new File(filePath)); | ||
| 83 | + out.write(bitmapStream.toByteArray()); | ||
| 84 | + bitmapStream.close(); | ||
| 85 | + out.flush(); | ||
| 86 | + out.close(); | ||
| 87 | + } | ||
| 88 | + return filePath; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + public static String getImagePath(Context context){ | ||
| 92 | + String status = Environment.getExternalStorageState(); | ||
| 93 | + String path = ""; | ||
| 94 | + if (status.equals(Environment.MEDIA_MOUNTED)) { | ||
| 95 | + String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + IMAGE_PATH; | ||
| 96 | + File file = new File(rootPath); | ||
| 97 | + if(!file.exists()){ | ||
| 98 | + file.mkdirs(); | ||
| 99 | + } | ||
| 100 | + path = rootPath + System.currentTimeMillis() + ".png"; | ||
| 101 | + }else { | ||
| 102 | + Toast.makeText(context, "未插入SD卡", Toast.LENGTH_SHORT).show(); | ||
| 103 | + } | ||
| 104 | + return path; | ||
| 105 | + | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + @TargetApi(Build.VERSION_CODES.KITKAT) | ||
| 109 | + public static String getPath(final Context context, final Uri uri) { | ||
| 110 | + | ||
| 111 | + final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; | ||
| 112 | + | ||
| 113 | + // DocumentProvider | ||
| 114 | + if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { | ||
| 115 | + // ExternalStorageProvider | ||
| 116 | + if (isExternalStorageDocument(uri)) { | ||
| 117 | + final String docId = DocumentsContract.getDocumentId(uri); | ||
| 118 | + final String[] split = docId.split(":"); | ||
| 119 | + final String type = split[0]; | ||
| 120 | + | ||
| 121 | + if ("primary".equalsIgnoreCase(type)) { | ||
| 122 | + return Environment.getExternalStorageDirectory() + "/" + split[1]; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + // TODO handle non-primary volumes | ||
| 126 | + } | ||
| 127 | + // DownloadsProvider | ||
| 128 | + else if (isDownloadsDocument(uri)) { | ||
| 129 | + | ||
| 130 | + final String id = DocumentsContract.getDocumentId(uri); | ||
| 131 | + final Uri contentUri = ContentUris.withAppendedId( | ||
| 132 | + Uri.parse("content://downloads/public_downloads"), Long.parseLong(id)); | ||
| 133 | + | ||
| 134 | + return getDataColumn(context, contentUri, null, null); | ||
| 135 | + } | ||
| 136 | + // MediaProvider | ||
| 137 | + else if (isMediaDocument(uri)) { | ||
| 138 | + final String docId = DocumentsContract.getDocumentId(uri); | ||
| 139 | + final String[] split = docId.split(":"); | ||
| 140 | + final String type = split[0]; | ||
| 141 | + | ||
| 142 | + Uri contentUri = null; | ||
| 143 | + if ("image".equals(type)) { | ||
| 144 | + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | ||
| 145 | + } else if ("video".equals(type)) { | ||
| 146 | + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; | ||
| 147 | + } else if ("audio".equals(type)) { | ||
| 148 | + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + final String selection = "_id=?"; | ||
| 152 | + final String[] selectionArgs = new String[]{ | ||
| 153 | + split[1] | ||
| 154 | + }; | ||
| 155 | + | ||
| 156 | + return getDataColumn(context, contentUri, selection, selectionArgs); | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + // MediaStore (and general) | ||
| 160 | + else if ("content".equalsIgnoreCase(uri.getScheme())) { | ||
| 161 | + | ||
| 162 | + // Return the remote address | ||
| 163 | + if (isGooglePhotosUri(uri)) | ||
| 164 | + return uri.getLastPathSegment(); | ||
| 165 | + | ||
| 166 | + return getDataColumn(context, uri, null, null); | ||
| 167 | + } | ||
| 168 | + // File | ||
| 169 | + else if ("file".equalsIgnoreCase(uri.getScheme())) { | ||
| 170 | + return uri.getPath(); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + return null; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + /** | ||
| 177 | + * Get the value of the data column for this Uri. This is useful for | ||
| 178 | + * MediaStore Uris, and other file-based ContentProviders. | ||
| 179 | + * | ||
| 180 | + * @param context The context. | ||
| 181 | + * @param uri The Uri to query. | ||
| 182 | + * @param selection (Optional) Filter used in the query. | ||
| 183 | + * @param selectionArgs (Optional) Selection arguments used in the query. | ||
| 184 | + * @return The value of the _data column, which is typically a file path. | ||
| 185 | + */ | ||
| 186 | + public static String getDataColumn(Context context, Uri uri, String selection, | ||
| 187 | + String[] selectionArgs) { | ||
| 188 | + | ||
| 189 | + Cursor cursor = null; | ||
| 190 | + final String column = "_data"; | ||
| 191 | + final String[] projection = { | ||
| 192 | + column | ||
| 193 | + }; | ||
| 194 | + | ||
| 195 | + try { | ||
| 196 | + cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, | ||
| 197 | + null); | ||
| 198 | + if (cursor != null && cursor.moveToFirst()) { | ||
| 199 | + final int index = cursor.getColumnIndexOrThrow(column); | ||
| 200 | + return cursor.getString(index); | ||
| 201 | + } | ||
| 202 | + } finally { | ||
| 203 | + if (cursor != null) | ||
| 204 | + cursor.close(); | ||
| 205 | + } | ||
| 206 | + return null; | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + /** | ||
| 210 | + * @param uri The Uri to check. | ||
| 211 | + * @return Whether the Uri authority is ExternalStorageProvider. | ||
| 212 | + */ | ||
| 213 | + public static boolean isExternalStorageDocument(Uri uri) { | ||
| 214 | + return "com.android.externalstorage.documents".equals(uri.getAuthority()); | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + /** | ||
| 218 | + * @param uri The Uri to check. | ||
| 219 | + * @return Whether the Uri authority is DownloadsProvider. | ||
| 220 | + */ | ||
| 221 | + public static boolean isDownloadsDocument(Uri uri) { | ||
| 222 | + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + /** | ||
| 226 | + * @param uri The Uri to check. | ||
| 227 | + * @return Whether the Uri authority is MediaProvider. | ||
| 228 | + */ | ||
| 229 | + public static boolean isMediaDocument(Uri uri) { | ||
| 230 | + return "com.android.providers.media.documents".equals(uri.getAuthority()); | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + /** | ||
| 234 | + * @param uri The Uri to check. | ||
| 235 | + * @return Whether the Uri authority is Google Photos. | ||
| 236 | + */ | ||
| 237 | + public static boolean isGooglePhotosUri(Uri uri) { | ||
| 238 | + return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + | ||
| 242 | + public static int readPictureDegree(String path) { | ||
| 243 | + int degree = 0; | ||
| 244 | + try { | ||
| 245 | + ExifInterface exifInterface = new ExifInterface(path); | ||
| 246 | + int orientation = exifInterface.getAttributeInt( | ||
| 247 | + ExifInterface.TAG_ORIENTATION, | ||
| 248 | + ExifInterface.ORIENTATION_NORMAL); | ||
| 249 | + switch (orientation) { | ||
| 250 | + case ExifInterface.ORIENTATION_ROTATE_90: | ||
| 251 | + degree = 90; | ||
| 252 | + break; | ||
| 253 | + case ExifInterface.ORIENTATION_ROTATE_180: | ||
| 254 | + degree = 180; | ||
| 255 | + break; | ||
| 256 | + case ExifInterface.ORIENTATION_ROTATE_270: | ||
| 257 | + degree = 270; | ||
| 258 | + break; | ||
| 259 | + default: | ||
| 260 | + break; | ||
| 261 | + } | ||
| 262 | + } catch (IOException e) { | ||
| 263 | + e.printStackTrace(); | ||
| 264 | + } | ||
| 265 | + return degree; | ||
| 266 | + } | ||
| 267 | + | ||
| 268 | + public static Bitmap rotate(Bitmap b, int degrees) { | ||
| 269 | + if (degrees == 0) { | ||
| 270 | + return b; | ||
| 271 | + } | ||
| 272 | + if (degrees != 0 && b != null) { | ||
| 273 | + Matrix m = new Matrix(); | ||
| 274 | + m.setRotate(degrees, (float) b.getWidth(), (float) b.getHeight()); | ||
| 275 | + try { | ||
| 276 | + Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), | ||
| 277 | + b.getHeight(), m, true); | ||
| 278 | + if (b != b2) { | ||
| 279 | + b.recycle(); // Android开发网再次提示Bitmap操作完应该显示的释放 | ||
| 280 | + b = b2; | ||
| 281 | + } | ||
| 282 | + } catch (OutOfMemoryError ex) { | ||
| 283 | + // Android123建议大家如何出现了内存不足异常,最好return 原始的bitmap对象。. | ||
| 284 | + } | ||
| 285 | + } | ||
| 286 | + return b; | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + public interface SubmitImageListener{ | ||
| 290 | + void writeComplete(File file); | ||
| 291 | + void writeError(String msg); | ||
| 292 | + } | ||
| 293 | +} |