上传头像出现内存溢出
来源:互联网 发布:阿里云 iaas paas 编辑:程序博客网 时间:2024/06/01 10:46
最近在做个人中心的时候,需要实现上传头像的功能,上传头像有两种,一种是通过选择本地相册里面的图片,第二种就是通过照相上传。在上传的途中会出现内存溢出的现象,如何解决呢?图片上传出现内存溢出无非就是上传的图片过大,如果对图片采取压缩则可避免因图片过大而造成的内存溢出。实现代码如下:
public class PersonMessageAty extends Activity implements OnClickListener {
private Button btnCamera, btnPicture, btnCancle, btn_finish,
btn_person_info_back;
ImageView img_nickname, img_phone, img_qq, img_address, img_icon;
TextView tv_nickname, tv_email, tv_phone, tv_qq, tv_address;
private String type;
private String uid, email;
private String edit_nickname, edit_phone, edit_address, edit_userpic,
edit_oicq;
final int PERSON_INFO = 100;
final int PERSON_MESSAGE = 1;
private PopupWindow mPopupWindow;
private String filePath = “”; // 全路径
private Bitmap mBitmapPhoto;// 共享的照片
/* 用来标识请求相册的activity /
private static final int TAKE_SMALL_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
private static final int CHOOSE_BIG_PICTURE = 3;
private Uri mImageUri;
private static final String IMAGE_FILE_LOCATION = “file:///sdcard/temp.jpg”;
DisplayImageOptions options;
private SharedPreferences preferences;
private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { /** * 至于下面这个Intent的ACTION是怎么知道的,大家可以看下自己路径下的如下网页 * yourself_sdk_path/docs/reference/android/content/Intent.html * 直接在里面Ctrl+F搜:CROP ,之前没仔细看过,其实安卓系统早已经有自带图片裁剪功能, 是直接调本地库的 */ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); // 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 是裁剪图片宽高 intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, requestCode);}@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.personal_info); preferences = getSharedPreferences("Share", Context.MODE_PRIVATE); uid = preferences.getString("uid", null); init(); options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_empty) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_empty).cacheInMemory(true) .cacheOnDisk(true).considerExifParams(true) // .displayer(new RoundedBitmapDisplayer(20)) .build(); mImageUri = Uri.parse(IMAGE_FILE_LOCATION); getPersonDataToNet();}public void init() { img_nickname = (ImageView) findViewById(R.id.img_nickname); img_phone = (ImageView) findViewById(R.id.img_phone); img_qq = (ImageView) findViewById(R.id.img_qq); img_address = (ImageView) findViewById(R.id.img_address); img_icon = (ImageView) findViewById(R.id.img_icon); tv_nickname = (TextView) findViewById(R.id.text_nickname); tv_email = (TextView) findViewById(R.id.text_email); tv_phone = (TextView) findViewById(R.id.text_phone); tv_qq = (TextView) findViewById(R.id.text_qq); tv_address = (TextView) findViewById(R.id.text_address); btn_finish = (Button) findViewById(R.id.btn_finish); btn_person_info_back = (Button) findViewById(R.id.btn_person_info_back); img_nickname.setOnClickListener(this); img_icon.setOnClickListener(this); img_phone.setOnClickListener(this); img_qq.setOnClickListener(this); img_address.setOnClickListener(this); btn_finish.setOnClickListener(this); btn_person_info_back.setOnClickListener(this);}@Overridepublic void onClick(View view) { Intent intent = new Intent(PersonMessageAty.this, PersonEditAty.class); intent.putExtra("uid", uid); switch (view.getId()) { case R.id.img_nickname: intent.putExtra("name", "昵称:"); intent.putExtra("message", edit_nickname); startActivityForResult(intent, 1); break; case R.id.img_phone: intent.putExtra("name", "手机号:"); intent.putExtra("message", edit_phone); startActivityForResult(intent, 2); break; case R.id.img_qq: intent.putExtra("name", "QQ:"); intent.putExtra("message", edit_oicq); startActivityForResult(intent, 3); break; case R.id.img_address: intent.putExtra("name", "地址:"); intent.putExtra("message", edit_address); startActivityForResult(intent, 4); break; case R.id.btn_finish: getDataFromServer(); break; case R.id.btnCamera: mPopupWindow.dismiss(); getPicByTakePhoto(); break; case R.id.btnPicture: mPopupWindow.dismiss(); intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 350); intent.putExtra("outputY", 350); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", false); startActivityForResult(intent, CHOOSE_BIG_PICTURE); break; case R.id.btnCancle: mPopupWindow.dismiss(); break; case R.id.img_icon: if (mPopupWindow == null || (mPopupWindow != null && !mPopupWindow.isShowing())) { showBottoPopupWindow(); } else { mPopupWindow.dismiss(); } break; case R.id.btn_person_info_back: finish(); break; }}protected void getPicByTakePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); startActivityForResult(intent, TAKE_SMALL_PICTURE);}private void setImagView(String path) { BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 1; // 获取图片的旋转角度,有些系统把拍照的图片旋转了,有的没有旋转 int degree = readPictureDegree(path); Bitmap bitmap = BitmapFactory.decodeFile(path, bitmapOptions); // 把图片旋转为正的方向 mBitmapPhoto = rotaingImageView(degree, bitmap); img_icon.setImageBitmap(mBitmapPhoto);}public Bitmap rotaingImageView(int angle, Bitmap bitmap) { // 旋转图片 动作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap;}private int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree;}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data != null) { if (resultCode == PERSON_INFO && requestCode == 1) { edit_nickname = data.getExtras().getString("content"); tv_nickname.setText(edit_nickname); } else if (resultCode == PERSON_INFO && requestCode == 2) { edit_phone = data.getExtras().getString("content"); tv_phone.setText(edit_phone); } else if (resultCode == PERSON_INFO && requestCode == 3) { edit_oicq = data.getExtras().getString("content"); tv_qq.setText(edit_oicq); } else if (resultCode == PERSON_INFO && requestCode == 4) { edit_address = data.getExtras().getString("content"); tv_address.setText(edit_address); } } if (resultCode != Activity.RESULT_OK) { return; } else { switch (requestCode) { case TAKE_SMALL_PICTURE: cropImageUri(mImageUri, 700, 700, CROP_SMALL_PICTURE); break; case CROP_SMALL_PICTURE: filePath = "sdcard/temp.jpg"; setImagView(filePath); break; case CHOOSE_BIG_PICTURE: filePath = "sdcard/temp.jpg"; setImagView(filePath); break; } } super.onActivityResult(requestCode, resultCode, data);}public void showBottoPopupWindow() { LayoutInflater mLayoutInfalter = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); View menuView = mLayoutInfalter .inflate(R.layout.share_choice_pic, null); mPopupWindow = new PopupWindow(menuView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); mPopupWindow.setAnimationStyle(R.style.AnimationPreview); mPopupWindow.showAtLocation(findViewById(R.id.personalinfo), Gravity.BOTTOM, 0, 0); mPopupWindow.setFocusable(true); mPopupWindow.setOutsideTouchable(false); btnCamera = (Button) menuView.findViewById(R.id.btnCamera); btnPicture = (Button) menuView.findViewById(R.id.btnPicture); btnCancle = (Button) menuView.findViewById(R.id.btnCancle); btnCamera.setOnClickListener(this); btnPicture.setOnClickListener(this); btnCancle.setOnClickListener(this);}private void getPersonDataToNet() { type = "edit"; RequestQueue requestQueue = Volley.getQueueInstance(this); // 网络请求方式 final StringRequest request = new StringRequest(Method.POST, ClassifyParser.urlJoint(getString(R.string.person_url), ClassifyParser.getPersonParams(R.string.person_url, uid, type)), new Listener<String>() { @Override // 根据的网络数据进行判断 public void onResponse(String response) { Object obj = new PersonParser().parseToObject(response); // 错误的数据解析 if (obj instanceof ErrorBean) { ErrorBean error = (ErrorBean) obj; Toast.makeText(getApplicationContext(), error.message, Toast.LENGTH_SHORT).show(); } else if (obj instanceof PersonBean) { email = PersonParser.email; edit_phone = PersonParser.phone; edit_oicq = PersonParser.oicq; edit_address = PersonParser.address; if (PersonParser.nickname.contains("?") || TextUtils.isEmpty(PersonParser.nickname)) { edit_nickname = preferences.getString( "nickName", null); edit_userpic = preferences.getString( "UserIcon", null); } else { edit_nickname = PersonParser.nickname; edit_userpic = PersonParser.userpic; } tv_nickname.setText(edit_nickname); tv_email.setText(email); tv_phone.setText(edit_phone); tv_qq.setText(edit_oicq); tv_address.setText(edit_address); if (edit_userpic.contains("upload")) { ImageLoader.getInstance().displayImage( Constants.SHAREPIC_URL + edit_userpic, img_icon, options); } else if (edit_userpic.contains("qzapp.qlogo.cn")) { GameDetailUtilClass.setImg_volley( PersonMessageAty.this, img_icon, 0, R.drawable.ic_empty, edit_userpic); } else if (edit_userpic.contains("ww1.sinaimg.cn")) { GameDetailUtilClass.setImg_volley( PersonMessageAty.this, img_icon, 0, R.drawable.ic_empty, edit_userpic); } else { GameDetailUtilClass.setImg_volley( PersonMessageAty.this, img_icon, 0, R.drawable.ic_empty, Constants.PERSON_URL + edit_userpic); } } } }, new ErrorListener() {// 解析错误的监听事件 @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show(); } }) { protected Map<String, String> getParams() throws AuthFailureError { return ClassifyParser.getLoginParams(R.string.person_url, uid, type); } }; requestQueue.add(request);}public void getDataFromServer() { type = "editok"; AsyncHttpClient client = new AsyncHttpClient(); com.anjoyo.net.RequestParams requestParams = ClassifyParser .getPersonEditParams(PersonMessageAty.this, R.string.person_url, uid, type, edit_nickname, edit_phone, edit_oicq, edit_address); if (filePath != null && filePath.length() > 0) { requestParams.put("userpic", BitmapToInputStream(mBitmapPhoto)); } client.post(getString(R.string.person_url), requestParams, new JsonHttpResponseHandler() { @Override public void onStart() { super.onStart(); } @Override public void onFinish() { super.onFinish(); } @Override public void onSuccess(JSONObject response) { super.onSuccess(response); Object obj = new PersonMessageParser() .parseJSON(response); if (obj instanceof PersonBean) { Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(PersonMessageAty.this, PersonalAty.class); startActivity(intent); finish(); } else if (obj instanceof ErrorBean) { Intent intent = new Intent(PersonMessageAty.this, PersonalAty.class); intent.putExtra("nickname", edit_nickname); PersonMessageAty.this.setResult(PERSON_MESSAGE, intent); finish(); } } @Override public void onFailure(Throwable error, String content) { super.onFailure(error, content); if (error instanceof java.net.ConnectException) { // 连接错误 Toast.makeText(getApplicationContext(), "连接错误", Toast.LENGTH_SHORT).show(); } else if (error instanceof HttpResponseException) { // 服务器接口错误 Toast.makeText(getApplicationContext(), "服务器接口错误", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show(); } } });}private InputStream BitmapToInputStream(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 80, baos); InputStream ist = new ByteArrayInputStream(baos.toByteArray()); return ist;}
}
- 上传头像出现内存溢出
- 上传头像内存溢出问题
- 上传头像
- 上传头像
- 上传头像
- 上传头像
- 头像上传
- 上传头像
- 头像上传
- 头像上传
- 头像上传
- 上传头像
- 上传头像
- 头像上传
- 头像上传
- 上传头像
- 头像上传
- 上传头像
- S2SH+ajax+json-------(一)
- 文章标题
- svn的hooks使用
- php 网上支付之易宝支付
- 各种排序算法时间复杂度和空间复杂度表
- 上传头像出现内存溢出
- Andorid Unable to execute dex: Multiple dex files define Lorg/apache/commons/collections/Buffer
- 64位Sql Server 2005开发版于64位Windows7旗舰版 安装过程
- ffmpeg:视频解码以及解码后数据的编码
- 从源码注释看 Activity 的生命周期方法
- ETL利器Kettle实战应用
- 关于 Javascript 的闭包
- Gson解析泛型
- spring注解( @Autowired、@Qualifier、@Resource、@PostConstruct、@PreDestroy、 @Component、@Scope)-描述的比较清楚