Android新浪微博之发送微博

本文介绍了一个简单的Android应用案例,展示了如何使用系统接口实现拍照功能,并将拍摄的照片上传至新浪微博。通过具体的代码示例,读者可以了解到整个过程的技术细节。

发送微博主要就是调用新浪微博给的接口、调用android拍照和对拍照后的图片处理。

拍照时调用的系统接口代码是:

Intent takephoto = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(takephoto, 520);


获得拍得的照片代码:

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
}


下面上全部代码:

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/titlebar_edit" />

    <EditText
        android:id="@+id/exit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@null"
        android:gravity="left|top"
        android:hint="分享新鲜事..."
        android:inputType="textMultiLine"
        android:textColor="#6f6e6e" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/img1"
            android:layout_width="150dip"
            android:layout_height="150dip" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginTop="10dip"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/takephoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dip"
            android:layout_marginRight="17dip"
            android:background="@drawable/edit_bottom_btn_takephoto" />

        <Button
            android:id="@+id/localphoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="17dip"
            android:layout_marginRight="17dip"
            android:background="@drawable/edit_bottom_btn_localphoto" />

        <Button
            android:id="@+id/mention"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="17dip"
            android:layout_marginRight="17dip"
            android:background="@drawable/edit_bottom_btn_mention" />

        <Button
            android:id="@+id/trend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="17dip"
            android:layout_marginRight="17dip"
            android:background="@drawable/edit_bottom_btn_trend" />

        <Button
            android:id="@+id/emo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="17dip"
            android:layout_marginRight="30dip"
            android:background="@drawable/edit_bottom_btn_emo" />
    </LinearLayout>

</LinearLayout>


WeiboEditActivity.java

package com.weibo.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.demo.AccessTokenKeeper;
import com.sina.weibo.sdk.demo.openapi.WBStatusAPIActivity;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.utils.LogUtil;
import com.soft.weibo.sdk.openapi.StatusesAPI;
import com.soft.weibo.sdk.openapi.models.ErrorInfo;
import com.soft.weibo.sdk.openapi.models.Status;
import com.soft.weibo.sdk.openapi.models.StatusList;

public class WeiboEditActivity extends Activity {

	private Intent intent;
	private Button btn_cancel;
	private Button btn_send;
	private Oauth2AccessToken mAccessToken;
	private EditText mEditText;
	private Button takePhoto;
	private Button localPhoto;
	private ImageView imgImg;
	private Bitmap bitmap;
	private String imageFilePath = null;
	private LinearLayout linearlayout;
	/** 用于获取微博信息流等操作的API */
	private StatusesAPI mStatusesAPI;
	private static final String TAG = WBStatusAPIActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit);
		this.intent = super.getIntent();
		// 获取当前已保存过的 Token
		mAccessToken = AccessTokenKeeper.readAccessToken(this);
		// 对statusAPI实例化
		mStatusesAPI = new StatusesAPI(mAccessToken);
		init();
		eListener();
	}

	/**
	 * 初始化
	 */
	private void init() {
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_send = (Button) findViewById(R.id.btn_send);
		mEditText = (EditText) findViewById(R.id.exit_text);
		takePhoto = (Button) findViewById(R.id.takephoto);
		localPhoto = (Button) findViewById(R.id.localphoto);
		imgImg = (ImageView) findViewById(R.id.img1);
		linearlayout = (LinearLayout)findViewById(R.id.linearlayout);
	}

	/**
	 * 按钮监听
	 */
	private void eListener() {
		// 取消发送
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				toHomeWeibo();
			}

		});
		// 发送微博
		btn_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendWeibo();
				toHomeWeibo();
			}

		});
		// 拍照
		takePhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				Intent takephoto = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(takephoto, 520);
			}
		});
		// 读取本地图片
		localPhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent localphotot = new Intent();
				localphotot.setAction(Intent.ACTION_GET_CONTENT);
				localphotot.setType("image/*");
				startActivityForResult(localphotot, 521);
			}
		});
	}

	/**
	 * 取回图片
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// 拍照片
		if (requestCode == 520) {

			if (resultCode == Activity.RESULT_OK) {
				String sdStatus = Environment.getExternalStorageState();
				if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {// 判断SD卡是否存在

					Log.v("SDCARD",
							"SD card is not avaiable/writeable right now");
					return;

				}
				
				Bundle bundle = new Bundle();
				bundle = data.getExtras();
				bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式

				FileOutputStream fileout = null;
				File file = new File("/sdcard/myImage/");
				file.mkdir();// 创建文件夹
				String filename = "/sdcard/myImage/111.jpg";

				try {
					fileout = new FileOutputStream(filename);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileout);
				} catch (FileNotFoundException e) {
					// TODO: handle exception
					e.printStackTrace();
				} finally {
					try {
						fileout.close();
						fileout.flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				linearlayout.setVisibility(View.VISIBLE);
				imgImg.setImageBitmap(bitmap);// 将图片显示在ImageView里

			}

		} else if (requestCode == 520) {
			if (requestCode == RESULT_OK) {

			}

		}

	}

	/**
	 * 发送微博
	 */

	private void sendWeibo() { // TODO Auto-generated method stub
		String msg = null;
		msg = mEditText.getText().toString();
		if (mAccessToken != null && mAccessToken.isSessionValid()) {
			// mStatusesAPI.update(msg, null, null, mListener);
			mStatusesAPI.upload(msg, bitmap, "12225", "4112", mListener);
			Log.e("sendweibo", "send");

		} else {
			Toast.makeText(WeiboEditActivity.this,
					R.string.weibosdk_demo_access_token_is_empty,
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 跳转到主页
	 */
	private void toHomeWeibo() {
		// TODO Auto-generated method stub
		intent = new Intent(this, WeiboMainActivity.class);
		startActivity(intent);

	}

	/**
	 * 微博 OpenAPI 回调接口。
	 */
	private RequestListener mListener = new RequestListener() {
		@Override
		public void onComplete(String response) {
			if (!TextUtils.isEmpty(response)) {
				LogUtil.i(TAG, response);
				if (response.startsWith("{\"statuses\"")) {
					// 调用 StatusList#parse 解析字符串成微博列表对象
					StatusList statuses = StatusList.parse(response);
					if (statuses != null && statuses.total_number > 0) {
						Toast.makeText(WeiboEditActivity.this,
								"获取微博信息流成功, 条数: " + statuses.statusList.size(),
								Toast.LENGTH_LONG).show();
					}
				} else if (response.startsWith("{\"created_at\"")) {
					// 调用 Status#parse 解析字符串成微博对象
					Status status = Status.parse(response);
					Toast.makeText(WeiboEditActivity.this,
							"发送一送微博成功, id = " + status.id, Toast.LENGTH_LONG)
							.show();
				} else {
					Toast.makeText(WeiboEditActivity.this, response,
							Toast.LENGTH_LONG).show();
				}
			}
		}

		@Override
		public void onWeiboException(WeiboException e) {
			LogUtil.e(TAG, e.getMessage());
			ErrorInfo info = ErrorInfo.parse(e.getMessage());
			Toast.makeText(WeiboEditActivity.this, info.toString(),
					Toast.LENGTH_LONG).show();
		}
	};

}


图片效果为:

 

需求响应动态冰蓄冷系统与需求响应策略的优化研究(Matlab代码实现)内容概要:本文围绕“需求响应动态冰蓄冷系统与需求响应策略的优化研究”展开,基于Matlab代码实现,重点探讨了冰蓄冷系统在电力需求响应背景下的动态建模与优化调度策略。研究结合实际电力负荷与电价信号,构建系统能耗模型,利用优化算法对冰蓄冷系统的运行策略进行求解,旨在降低用电成本、平衡电网负荷,并提升能源利用效率。文中还提及该研究为博士论文复现,涉及系统建模、优化算法应用与仿真验证等关键技术环节,配套提供了完整的Matlab代码资源。; 适合人群:具备一定电力系统、能源管理或优化算法基础,从事科研或工程应用的研究生、高校教师及企业研发人员,尤其适合开展需求响应、综合能源系统优化等相关课题研究的人员。; 使用场景及目标:①复现博士论文中的冰蓄冷系统需求响应优化模型;②学习Matlab在能源系统建模与优化中的具体实现方法;③掌握需求响应策略的设计思路与仿真验证流程,服务于科研项目、论文写作或实际工程方案设计。; 阅读建议:建议结合提供的Matlab代码逐模块分析,重点关注系统建模逻辑与优化算法的实现细节,按文档目录顺序系统学习,并尝试调整参数进行仿真对比,以深入理解不同需求响应策略的效果差异。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值