標籤:android 圖片上傳
上一篇我們介紹了如何由uri轉換成String ,本文就用到了上篇文章的方法,下面我們介紹一下如何設定頭像後將頭像圖片上傳到雲端的方法,本文基於Bmob提供的服務。
看一下代碼:(布局檔案和前兩篇文章依舊一樣,不再提供)
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;import 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, "8f3ffb2658d8a3366a70a0b0ca0b71b2");//初始化BmobsetupViews();}private void setupViews() {mImageHeader = (ImageView) 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);galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);galleryIntent.setType("image/*");//圖片startActivityForResult(galleryIntent, IMAGE_REQUEST_CODE);break;case R.id.btn_takephoto:if (isSdcardExisting()) {Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");//拍照cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);} else {Toast.makeText(v.getContext(), "請插入sd卡", 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();//擷取圖片uriresizeImage(originalUri);//下面方法將擷取的uri轉為String類型哦!String []imgs1={MediaStore.Images.Media.DATA};//將圖片URI轉換成儲存路徑 Cursor cursor=this.managedQuery(originalUri, imgs1, null, null, 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={MediaStore.Images.Media.DATA};//將圖片URI轉換成儲存路徑 Cursor cursor1=this.managedQuery(getImageUri(), imgs, null, null, 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, "未找到儲存卡,無法儲存照片!",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() {//判斷SD卡是否存在final String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {return true;} else {return false;}}public void resizeImage(Uri uri) {//重塑圖片大小Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");//可以裁剪intent.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 data) {//顯示圖片Bundle extras = data.getExtras();if (extras != null) {Bitmap photo = extras.getParcelable("data");Drawable drawable = new BitmapDrawable(photo);mImageHeader.setImageDrawable(drawable);}}private Uri getImageUri() {//擷取路徑return Uri.fromFile(new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME));}/** * 將圖片上傳 * @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("圖片上傳成功");}@Overridepublic void onProgress(Integer arg0) {// TODO Auto-generated method stub}@Overridepublic void onFailure(int arg0, String arg1) {// TODO Auto-generated method stubshowToast("圖片上傳失敗:"+arg1);}});}}
javabean類:
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;}}
運行執行個體,又再次出現問題:
還是許可權問題:
然後再運行:
提示圖片上傳成功,我們看一下後台:
OK,圖片已經上傳成功嘍!是不是很吊?
歡迎大家轉載、交流、點贊、關注!您的支援是我的動力!!!!
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android實戰簡易教程-第二十八槍(基於Bmob實現頭像圖片設定和網路上傳功能!)