首页 > 代码库 > 如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location) (一)

如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location) (一)

先上效果图:



先看页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	android:orientation="vertical"
	android:background="@android:color/white" >
	
	<RelativeLayout
		android:layout_width="fill_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:background="#f4f4f4" >
		
		<LinearLayout
			android:id="@+id/btns_bottom"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_alignParentBottom="true"
			android:orientation="horizontal">
			
			
			<LinearLayout <strong> //这5个是底部的按钮</strong>
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:layout_weight="1">
				<ImageButton
					android:src=http://www.mamicode.com/"@drawable/btn_insert_save">

首先看从手机相册里load image:

private void startToMediaActivity() {
		startActivityForResult(new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
				WriteBlog.REQUEST_CODE_GALLERY);
	}

private void startToCameraActivity() {
		if (FileUtils.isSdCardAvailable()) {
			final Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			final Uri picUri = Uri.fromFile(sdcardTempFile);
			i.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
			startActivityForResult(i, WriteBlog.REQUEST_CODE_CAMERA);
		}
		else {
			Toast.makeText(this, R.string.pls_insert_sdcard, Toast.LENGTH_SHORT)
					.show();
		}
	}
	

一个Image的处理类:

	final class ImageLoadingHelper {
		
		private File picfile;
		private WriteBlog activity;
		private String defaultTempPath;
		private String defaultDraftPath;
		
		Bitmap createThumbnail() {
			Options thumbnailOpts = new BitmapFactory.Options();
			if (!hasBitmap()) { throw new IllegalStateException("There is no pic!"); }
			return BitmapFactory.decodeFile(getTempPath(),
					thumbnailOpts);
		}
		
		synchronized boolean hasBitmap() {
			String picfileName = picfile.getName();
			if(picfileName.matches(".avi")|| picfileName.matches(".rm")||picfileName.matches(".mp4")
					||picfileName.matches(".rmvb")||picfileName.matches(".wmv")||picfileName.matches(".flv"))
				{
				return false;
				}
			else{
				return FileUtils.doesExisted(picfile);
			}
			
		}
		
		synchronized void importBitmapFile(Uri uri) {
			if (uri.getScheme().equals("content")) {
				InputStream inputStream = null;
				try {
					inputStream = activity.getContentResolver()
							.openInputStream(uri);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				FileUtils.file_put_contents(inputStream,picfile);
			}
		}
		
		
		boolean doesDraftExist() {
			return FileUtils.doesExisted(defaultDraftPath);
		}
		

		String getTempPath() {
			return picfile == null ? "" : picfile.getAbsolutePath();
		}
		
		
		void loadDraft() {
			FileUtils.copy(defaultDraftPath, defaultTempPath);
			activity.displayInsertBitmap();
			
		}
		
		void saveDraft() {
			FileUtils.copy(defaultTempPath, defaultDraftPath);
		}
		
		
		ImageLoadingHelper(WriteBlog activity) {
			this.activity = activity;
			if (FileUtils.isSdCardAvailable()) {
				defaultTempPath = FileUtils.getSDPath() + "/temp/"
						+ System.currentTimeMillis() + ".jpg";
			}
			else {
				defaultTempPath = activity.getFilesDir().getAbsolutePath()
						+ "/pic/" + System.currentTimeMillis() + ".jpg";
			}
			defaultDraftPath = activity.getFilesDir().getAbsolutePath()
					+ "/draft/bitmap_temp.jpg";
			if (FileUtils.doesExisted(defaultTempPath)) {
				defaultTempPath.replace(".jpg", "(1).jpg");
			}
			picfile = new File(defaultTempPath);
		}
		
	}



选完图片我们会调用onactivityResult:

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ((requestCode == WriteBlog.REQUEST_CODE_CAMERA)
				|| (WriteBlog.REQUEST_CODE_GALLERY == requestCode)) {
			if (resultCode != Activity.RESULT_OK) { return; }
			Uri uri = null;
			if ((requestCode == WriteBlog.REQUEST_CODE_CAMERA)
					&& FileUtils.doesExisted(sdcardTempFile)) {
				uri = Uri.fromFile(sdcardTempFile);
			}
			else if (WriteBlog.REQUEST_CODE_GALLERY == requestCode) {
				uri = data.getData();
			}

			if (uri != null) {
				displayImageUri(uri);
			}
		}
	}
	
	private void displayImageUri(Uri uri) {
		mImageLoadingHelper.importBitmapFile(uri);
		displayInsertBitmap();
	}
	
	void displayInsertBitmap() {
		insertPicView.setImageBitmap(mImageLoadingHelper
				.createThumbnail());
		insertPicView.setVisibility(View.VISIBLE);
	}