发送微博主要就是调用新浪微博给的接口、调用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();
}
};
}
图片效果为:
本文介绍了一个简单的Android应用案例,展示了如何使用系统接口实现拍照功能,并将拍摄的照片上传至新浪微博。通过具体的代码示例,读者可以了解到整个过程的技术细节。
242

被折叠的 条评论
为什么被折叠?



