|
13 | 13 | import android.provider.MediaStore;
|
14 | 14 | import android.support.annotation.Nullable;
|
15 | 15 | import android.support.v4.content.FileProvider;
|
| 16 | +import android.text.TextUtils; |
16 | 17 | import android.util.Log;
|
17 | 18 | import android.view.View;
|
18 | 19 | import android.view.ViewGroup;
|
|
32 | 33 | import com.rae.cnblogs.user.personal.UserAvatarPresenterImpl;
|
33 | 34 |
|
34 | 35 | import java.io.File;
|
| 36 | +import java.io.FileNotFoundException; |
35 | 37 | import java.io.FileOutputStream;
|
| 38 | +import java.io.IOException; |
36 | 39 | import java.io.OutputStream;
|
37 | 40 | import java.util.ArrayList;
|
38 | 41 |
|
@@ -128,18 +131,53 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
128 | 131 | }
|
129 | 132 |
|
130 | 133 | // 图片裁剪返回
|
131 |
| - if (resultCode == RESULT_OK && (requestCode == REQUEST_CODE_CROP || requestCode == REQUEST_CODE_CROP_FILE_PROVIDER) && data != null && data.getData() != null) { |
132 |
| - String path = data.getData().getPath(); |
133 |
| - Log.i("rae", "路径为:" + data.getData()); |
| 134 | + if (resultCode == RESULT_OK && (requestCode == REQUEST_CODE_CROP || requestCode == REQUEST_CODE_CROP_FILE_PROVIDER) && data != null) { |
| 135 | + String path = getResultImageData(data); |
| 136 | + if (TextUtils.isEmpty(path)) { |
| 137 | + UICompat.failed(this, "头像获取失败"); |
| 138 | + return; |
| 139 | + } |
| 140 | + Log.i("rae", "路径为:" + path); |
134 | 141 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
135 | 142 | path = handleContentUri(data.getData());
|
136 | 143 | }
|
| 144 | + |
137 | 145 | onAvatarImageChanged(path);
|
138 | 146 | handleUpload();
|
139 | 147 | }
|
140 | 148 |
|
141 | 149 | }
|
142 | 150 |
|
| 151 | + @Nullable |
| 152 | + private String getResultImageData(Intent data) { |
| 153 | + Uri uri = data.getData(); |
| 154 | + if (uri != null) return uri.toString(); |
| 155 | + if (data.getExtras() == null) return null; |
| 156 | + Object objData = data.getExtras().get("data"); |
| 157 | + if (objData instanceof String) return objData.toString(); |
| 158 | + if (objData instanceof Bitmap) { |
| 159 | + Bitmap bitmap = (Bitmap) objData; |
| 160 | + File file = new File(getExternalCacheDir(), "avatar-upload-" + System.currentTimeMillis() + ".png"); |
| 161 | + OutputStream fileStream = null; |
| 162 | + try { |
| 163 | + fileStream = new FileOutputStream(file); |
| 164 | + bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileStream); |
| 165 | + return file.getPath(); |
| 166 | + } catch (FileNotFoundException e) { |
| 167 | + e.printStackTrace(); |
| 168 | + } finally { |
| 169 | + if (fileStream != null) { |
| 170 | + try { |
| 171 | + fileStream.close(); |
| 172 | + } catch (IOException e) { |
| 173 | + e.printStackTrace(); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + return null; |
| 179 | + } |
| 180 | + |
143 | 181 | private String handleContentUri(Uri uri) {
|
144 | 182 | try {
|
145 | 183 | ParcelFileDescriptor fileDescriptor = this.getContentResolver().openFileDescriptor(uri, "r");
|
|
0 commit comments