Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android-對相冊中的圖片進行上傳或下載

Android-對相冊中的圖片進行上傳或下載

編輯:關於Android編程

目標效果:

\ \ \

打開相冊選擇一張圖片,會顯示到上方的ImageView中並存儲到Bmob中,存儲後進入Bmob後台,復制剛才添加的數據的objectId,粘貼到代碼指定出,然後運行,點擊下載會在下方的ImageView顯示剛才上傳的圖片,這裡的下載是指定objectId,可以進行動態獲取objectId進行下載。

 

1.activity_main.xml頁面設置布局。

activity_main.xml頁面:



	
    2.MainActivity.java頁面定義上傳與下載方法。 MainActivity.java頁面:
package com.example.text;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.DownloadFileListener;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UploadBatchListener;
import cn.bmob.v3.listener.UploadFileListener;

import com.android.volley.toolbox.ImageLoader;
import com.example.text.MainActivity;

import entity.Person;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView ivHead, ivDownload;
	private Button btnSelectImage, btnDownloadImage;

	private static final int IMAGE_CODE = 0;// 打開相冊
	private static final int RESIZE_CODE = 2;// 調整大小

	private static final String IMAGE_NAME = " ";// 圖片字符串

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Bmob.initialize(this, "8ef8a4752f48682eadb32d3c8c8e398f");// 初始化Bmob

		// 初始化控件
		init();

		// 綁定點擊事件
		bindClick();
	}

	// 初始化控件
	private void init() {
		ivHead = (ImageView) findViewById(R.id.ivHead);
		ivDownload = (ImageView) findViewById(R.id.ivDownload);
		btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
		btnDownloadImage = (Button) findViewById(R.id.btnDownloadImage);
	}

	// 綁定點擊事件
	private void bindClick() {
		btnSelectImage.setOnClickListener(this);
		btnDownloadImage.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnSelectImage:
			Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
			galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
			galleryIntent.setType("image/*");//圖片
			startActivityForResult(galleryIntent, IMAGE_CODE);   //跳轉,傳遞打開相冊請求碼
			break;
		case R.id.btnDownloadImage:
			BmobQuery query=new BmobQuery();
			query.getObject(this,"ef292ff6ef",new GetListener() {  //第二個參數為想要下載的BmobFile數據的objectId
				
				@Override
				public void onSuccess(Person person) {
					Toast.makeText(MainActivity.this,"獲取成功",Toast.LENGTH_SHORT).show();
					
					//下載圖片
					downloadImage(person);
				}
				
				@Override
				public void onFailure(int arg0, String arg1) {
					Toast.makeText(MainActivity.this,"獲取失敗",Toast.LENGTH_SHORT).show();
				}
			});
			break;
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		} else {
			switch (requestCode) {
			case IMAGE_CODE:
				Uri uri = data.getData();
				resizeImage(uri);
				// 將獲取到的uri轉換為String型
				String[] images = { MediaStore.Images.Media.DATA };// 將圖片URI轉換成存儲路徑
				Cursor cursor = this
						.managedQuery(uri, images, null, null, null);
				int index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToFirst();
				String img_url = cursor.getString(index);
				upload(img_url);
				break;

			case RESIZE_CODE:
				if (data != null) {
					showResizeImage(data);
				}
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	// 判斷SD卡是否存在
	private boolean isSdcardExisting() {
		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_CODE);// 跳轉,傳遞調整大小請求碼
	}

	// 獲取路徑
	private Uri getImageUri() {
		return Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
				IMAGE_NAME));
	}

	// 顯示圖片
	private void showResizeImage(Intent data) {
		Bundle extras = data.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
			Drawable drawable = new BitmapDrawable(photo);
			ivHead.setImageDrawable(drawable);
		}
	}

	// 圖片上傳
	private void upload(String imgpath) {
		final BmobFile icon = new BmobFile(new File(imgpath));
		icon.upload(this, new UploadFileListener() {

			@Override
			public void onSuccess() {
				Person person = new Person();
				person.setIcon(icon);
				person.save(MainActivity.this);
				Toast.makeText(MainActivity.this,"圖片上傳成功",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				Toast.makeText(MainActivity.this,"圖片上傳失敗",Toast.LENGTH_SHORT).show();
			}
		});
	}

	//下載圖片
	private void downloadImage(Person person) {
		BmobFile icon=person.getIcon();
		icon.download(MainActivity.this,new DownloadFileListener() {
			
			@Override
			public void onSuccess(String url) {
				ivDownload.setImageBitmap(BitmapFactory.decodeFile(url));   //根據地址解碼並顯示圖片
				
			}
			
			@Override
			public void onFailure(int arg0, String arg1) {
				Toast.makeText(MainActivity.this,"下載失敗",Toast.LENGTH_SHORT).show();
			}
		});
		
	}
}
  3.Person.java頁面為實體類,運行自動生成一個Person表,這個表只有一個字段存儲圖片。 Person.java頁面:
package entity;

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;
	}
	
}
    4.因為使用了Bmob,所以需要添加權限。 AndroidManifest.xml頁面:



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
        
            
                

                
            
        
    


  5.注意之前講過使用Bmob需要下載第三方SDK,將libs文件夾中的所有內容都復制到項目libs目錄下,然後Properties->Java Build Path->Libraries->Add JARs...添加剛復制的四個jar包 \

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved