首页 > 代码库 > 如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入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); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。