Simple tutorial for Android-28th guns (Avatar image setting and network Upload Based on Bmob !),

Source: Internet
Author: User

Simple tutorial for Android-28th guns (Avatar image setting and network Upload Based on Bmob !),

In the previous article, we introduced how to convert a uri to a String. This article uses the method in the previous article. Next we will introduce how to set an avatar and upload it to the cloud, this article is based on the services provided by Bmob.

Let's take a look at the Code: (the layout file is still the same as the previous two articles and is no longer provided)

Package com. example. userphoto; import java. io. file; import android. app. activity; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; imp Ort android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. toast; import cn. bmob. v3.Bmob; import cn. bmob. v3.datatype. bmobFile; import cn. bmob. v3.listener. uploadFileListener; public class MainActivity extends Activity implements OnClickListener {private static final int IMAGE_REQUEST_CODE = 0; private static final int CAMERA_REQUEST_CODE = 1; Private static final int RESIZE_REQUEST_CODE = 2; private static final String IMAGE_FILE_NAME = "header.jpg"; private ImageView mImageHeader; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); // initialize BmobsetupViews ();} private void setupViews () {mImageHeader = (Im AgeView) findViewById (R. id. image_header); final Button selectBtn1 = (Button) findViewById (R. id. btn_selectimage); final Button selectBtn2 = (Button) findViewById (R. id. btn_takephoto); selectBtn1.setOnClickListener (this); selectBtn2.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_selectimage: Intent galleryIntent = new Intent (Intent. ACTION_GET_CONTENT); gal LeryIntent. addCategory (Intent. CATEGORY_OPENABLE); galleryIntent. setType ("image/*"); // image startActivityForResult (galleryIntent, IMAGE_REQUEST_CODE); break; case R. id. btn_takephoto: if (isSdcardExisting () {Intent cameraIntent = new Intent ("android. media. action. IMAGE_CAPTURE "); // take a photo of cameraIntent. putExtra (MediaStore. EXTRA_OUTPUT, getImageUri (); cameraIntent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 0); startAc TivityForResult (cameraIntent, CAMERA_REQUEST_CODE);} else {Toast. makeText (v. getContext (), "insert SD card", Toast. LENGTH_LONG ). show () ;} break ;}}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode! = RESULT_ OK) {return;} else {switch (requestCode) {case IMAGE_REQUEST_CODE: Uri originalUri = data. getData (); // obtain the image uriresizeImage (originalUri); // The following method converts the retrieved uri to the String type! String [] imgs1 = {MediaStore. images. media. DATA}; // convert the image URI to the storage path Cursor cursor = this. managedQuery (originalUri, imgs1, null); int index = cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); cursor. moveToFirst (); String img_url = cursor. getString (index); upload (img_url); // showToast (img_url); break; case CAMERA_REQUEST_CODE: if (isSdcardExisting () {resizeImage (getImageUri ()); string [] imgs = {M EdiaStore. images. media. DATA}; // convert the image URI to the storage path Cursor cursor1 = this. managedQuery (getImageUri (), imgs, null); int index1 = cursor1.getColumnIndexOrThrow (MediaStore. images. media. DATA); cursor1.moveToFirst (); String img_url1 = cursor1.getString (index1); upload (img_url1); // showToast (img_url1);} else {Toast. makeText (MainActivity. this, "no memory card found, unable to store photos! ", Toast. LENGTH_LONG). show ();} break; case RESIZE_REQUEST_CODE: if (data! = Null) {showResizeImage (data) ;}break ;}} super. onActivityResult (requestCode, resultCode, data);} private void showToast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show ();} private boolean isSdcardExisting () {// determine whether the SD card has final String state = Environment. getExternalStorageState (); if (state. equals (Environment. MEDIA_MOUNTED) {return true;} else {return false;} public void resizeImage (Uri ur I) {// reshapes the image size Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); intent. putExtra ("crop", "true"); // intent can be cropped. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", 150); intent. putExtra ("outputY", 150); intent. putExtra ("return-data", true); startActivityForResult (intent, RESIZE_REQUEST_CODE);} private void showResizeImage (Intent d Ata) {// display image Bundle extras = data. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); Drawable drawable = new BitmapDrawable (photo); mImageHeader. setImageDrawable (drawable);} private Uri getImageUri () {// get the path return Uri. fromFile (new File (Environment. getExternalStorageDirectory (), IMAGE_FILE_NAME);}/*** upload an image * @ param imgpath */private void upload (String imgpath) {final BmobFile icon = new BmobFile (new File (imgpath); icon. upload (this, new UploadFileListener () {@ Overridepublic void onSuccess () {// TODO Auto-generated method stubPerson person = new Person (); person. setIcon (icon); person. save (MainActivity. this); showToast ("image uploaded successfully") ;}@ Overridepublic void onProgress (Integer arg0) {// TODO Auto-generated method stub} @ Overridepublic void onFailure (int arg0, string arg1) {// TODO Auto-generated method stubshowToast ("Image Upload Failed:" + arg1 );}});}}

Javabean class:

package com.example.userphoto;import cn.bmob.v3.BmobObject;import cn.bmob.v3.datatype.BmobFile;public class Person extends BmobObject {private BmobFile icon;public BmobFile getIcon() {return icon;}public void setIcon(BmobFile icon) {this.icon = icon;}}

Run the instance, and the problem occurs again:


Or permission issues:


Then run:


The system prompts that the image is successfully uploaded. Let's take a look at the background:




OK. The image has been uploaded! Is it very difficult?

You are welcome to repost, exchange, likes, and follow! Your support is my motivation !!!!


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.