Android:SNS客户端开发七:发送带图片的微博(一)(调用相机和Gallery获得照片)
2012-07-09 11:26
459 查看
之前已经介绍了如何发布一条文字微博,接下来的两篇文章会介绍如何发送带图片的微博。今天先看如何调用照相或者Gallery来获取我们想要发送图片文件。
第一步,看需要申明的几个值
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
private String picPath;//文件路径
private static final int PHOTO_WITH_CAMERA = 1010;// 拍摄照片
private static final int PHOTO_WITH_DATA = 1020;// 从SD中得到照片
private static final File PHOTO_DIR = new File(
Environment.getExternalStorageDirectory() + "/DCIM/Camera");//拍摄照片存储的文件夹路劲
private File capturefile;//拍摄的照片文件
第二步,选择获取图片方式的对话框实现
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
final Context dialogContext = new ContextThemeWrapper(context,
android.R.style.Theme_Light);
String[] choices;
choices = new String[2];
choices[0] = "相机拍摄"; // 拍照
choices[1] = "本地相册"; // 从相册中选择
final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
android.R.layout.simple_list_item_1, choices);
final AlertDialog.Builder builder = new AlertDialog.Builder(
dialogContext);
builder.setTitle("添加图片");
builder.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch (which) {
case 0: {
String status = Environment
.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
capturefile = new File(PHOTO_DIR,
getPhotoFileName());
try {
capturefile.createNewFile();
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivityForResult(i, PHOTO_WITH_CAMERA);// 用户点击了从照相机获取
} else {
showToast("没有SD卡");
}
break;
}
case 1:// 从相册中去获取
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, PHOTO_WITH_DATA);
break;
}
}
});
builder.create().show();
}
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 通过相机回传图片的文件名
*/
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
再来看OnActivityResult
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 选择图片的回传处理
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
File file = null;
Bitmap pic = null;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PHOTO_WITH_CAMERA://获取拍摄的文件
picPath = capturefile.getAbsolutePath();
System.out.println(picPath);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
System.out.println("++++++相机+++++");
break;
case PHOTO_WITH_DATA://获取从图库选择的文件
Uri uri = data.getData();
String scheme = uri.getScheme();
if (scheme.equalsIgnoreCase("file")) {
picPath = uri.getPath();
System.out.println(picPath);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
} else if (scheme.equalsIgnoreCase("content")) {
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
cursor.moveToFirst();
picPath = cursor.getString(1);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
这里需要注意的是,从Gallery返回的内容,分为了两种情况。在模拟器上,我们可以发现返回的内容模式为"content”而从某些手机的操作例如MIUI,返回的scheme为"file"。同样,在MIUI上返回的照相内容依旧为file,但是可以通过通用方法,将照相的信息写入到我们制定的文件当中。也就是上面代码中的这两句
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
capturefile = new File(PHOTO_DIR,
getPhotoFileName());
try {
capturefile.createNewFile();
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在之前的文章中,我们为发送微博页面设计了一个ImageView用来显示选取的照片。但是我们发现,如果不对返回的照片做处理,那么在第二次选择照片的时候系统会抛出内存溢出的错误。网上对这个问题的解释是,android只为每个程序分配8M的缓存,所以图片不经过压缩就会抛出异常。那么我们把图片压缩之后再显示在ImageView当中,压缩方法如下:
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 压缩图片,避免内存不足报错
*/
private Bitmap decodeFile(File f) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > 100 || o.outWidth > 100) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(100 / (double) Math.max(
o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
至此,我们完成了对图片的选择,下一篇文章会通过新浪指定的方法,实现将图片上传到微博,也就是发送一条带图片的微博。
转自:/article/4326187.html
第一步,看需要申明的几个值
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
private String picPath;//文件路径
private static final int PHOTO_WITH_CAMERA = 1010;// 拍摄照片
private static final int PHOTO_WITH_DATA = 1020;// 从SD中得到照片
private static final File PHOTO_DIR = new File(
Environment.getExternalStorageDirectory() + "/DCIM/Camera");//拍摄照片存储的文件夹路劲
private File capturefile;//拍摄的照片文件
private String picPath;//文件路径 private static final int PHOTO_WITH_CAMERA = 1010;// 拍摄照片 private static final int PHOTO_WITH_DATA = 1020;// 从SD中得到照片 private static final File PHOTO_DIR = new File( Environment.getExternalStorageDirectory() + "/DCIM/Camera");//拍摄照片存储的文件夹路劲 private File capturefile;//拍摄的照片文件
第二步,选择获取图片方式的对话框实现
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
final Context dialogContext = new ContextThemeWrapper(context,
android.R.style.Theme_Light);
String[] choices;
choices = new String[2];
choices[0] = "相机拍摄"; // 拍照
choices[1] = "本地相册"; // 从相册中选择
final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
android.R.layout.simple_list_item_1, choices);
final AlertDialog.Builder builder = new AlertDialog.Builder(
dialogContext);
builder.setTitle("添加图片");
builder.setSingleChoiceItems(adapter, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch (which) {
case 0: {
String status = Environment
.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
capturefile = new File(PHOTO_DIR,
getPhotoFileName());
try {
capturefile.createNewFile();
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivityForResult(i, PHOTO_WITH_CAMERA);// 用户点击了从照相机获取
} else {
showToast("没有SD卡");
}
break;
}
case 1:// 从相册中去获取
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, PHOTO_WITH_DATA);
break;
}
}
});
builder.create().show();
}
final Context dialogContext = new ContextThemeWrapper(context, android.R.style.Theme_Light); String[] choices; choices = new String[2]; choices[0] = "相机拍摄"; // 拍照 choices[1] = "本地相册"; // 从相册中选择 final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1, choices); final AlertDialog.Builder builder = new AlertDialog.Builder( dialogContext); builder.setTitle("添加图片"); builder.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch (which) { case 0: { String status = Environment .getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡 Intent i = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); capturefile = new File(PHOTO_DIR, getPhotoFileName()); try { capturefile.createNewFile(); i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } startActivityForResult(i, PHOTO_WITH_CAMERA);// 用户点击了从照相机获取 } else { showToast("没有SD卡"); } break; } case 1:// 从相册中去获取 Intent intent = new Intent(); /* 开启Pictures画面Type设定为image */ intent.setType("image/*"); /* 使用Intent.ACTION_GET_CONTENT这个Action */ intent.setAction(Intent.ACTION_GET_CONTENT); /* 取得相片后返回本画面 */ startActivityForResult(intent, PHOTO_WITH_DATA); break; } } }); builder.create().show(); }
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 通过相机回传图片的文件名
*/
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat(
"'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
/* * 通过相机回传图片的文件名 */ private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; }
再来看OnActivityResult
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 选择图片的回传处理
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
File file = null;
Bitmap pic = null;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PHOTO_WITH_CAMERA://获取拍摄的文件
picPath = capturefile.getAbsolutePath();
System.out.println(picPath);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
System.out.println("++++++相机+++++");
break;
case PHOTO_WITH_DATA://获取从图库选择的文件
Uri uri = data.getData();
String scheme = uri.getScheme();
if (scheme.equalsIgnoreCase("file")) {
picPath = uri.getPath();
System.out.println(picPath);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
} else if (scheme.equalsIgnoreCase("content")) {
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
cursor.moveToFirst();
picPath = cursor.getString(1);
file = new File(picPath);
pic = decodeFile(file);
thumbimage.setImageBitmap(pic);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/* * 选择图片的回传处理 */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { File file = null; Bitmap pic = null; if (resultCode == RESULT_OK) { switch (requestCode) { case PHOTO_WITH_CAMERA://获取拍摄的文件 picPath = capturefile.getAbsolutePath(); System.out.println(picPath); file = new File(picPath); pic = decodeFile(file); thumbimage.setImageBitmap(pic); System.out.println("++++++相机+++++"); break; case PHOTO_WITH_DATA://获取从图库选择的文件 Uri uri = data.getData(); String scheme = uri.getScheme(); if (scheme.equalsIgnoreCase("file")) { picPath = uri.getPath(); System.out.println(picPath); file = new File(picPath); pic = decodeFile(file); thumbimage.setImageBitmap(pic); } else if (scheme.equalsIgnoreCase("content")) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); picPath = cursor.getString(1); file = new File(picPath); pic = decodeFile(file); thumbimage.setImageBitmap(pic); } break; } } super.onActivityResult(requestCode, resultCode, data); }
这里需要注意的是,从Gallery返回的内容,分为了两种情况。在模拟器上,我们可以发现返回的内容模式为"content”而从某些手机的操作例如MIUI,返回的scheme为"file"。同样,在MIUI上返回的照相内容依旧为file,但是可以通过通用方法,将照相的信息写入到我们制定的文件当中。也就是上面代码中的这两句
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
capturefile = new File(PHOTO_DIR,
getPhotoFileName());
try {
capturefile.createNewFile();
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); capturefile = new File(PHOTO_DIR, getPhotoFileName()); try { capturefile.createNewFile(); i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(capturefile));//将拍摄的照片信息存到capturefile中 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
在之前的文章中,我们为发送微博页面设计了一个ImageView用来显示选取的照片。但是我们发现,如果不对返回的照片做处理,那么在第二次选择照片的时候系统会抛出内存溢出的错误。网上对这个问题的解释是,android只为每个程序分配8M的缓存,所以图片不经过压缩就会抛出异常。那么我们把图片压缩之后再显示在ImageView当中,压缩方法如下:
Java代码
![](http://river418.iteye.com/images/icon_copy.gif)
![](http://river418.iteye.com/images/icon_star.png)
![](http://river418.iteye.com/images/spinner.gif)
/*
* 压缩图片,避免内存不足报错
*/
private Bitmap decodeFile(File f) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > 100 || o.outWidth > 100) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(100 / (double) Math.max(
o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/* * 压缩图片,避免内存不足报错 */ private Bitmap decodeFile(File f) { Bitmap b = null; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > 100 || o.outWidth > 100) { scale = (int) Math.pow( 2, (int) Math.round(Math.log(100 / (double) Math.max( o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { e.printStackTrace(); } return b; }
至此,我们完成了对图片的选择,下一篇文章会通过新浪指定的方法,实现将图片上传到微博,也就是发送一条带图片的微博。
转自:/article/4326187.html
相关文章推荐
- Android:SNS客户端开发八:发送带图片的微博(二)(发送多媒体的post方法)
- Android腾讯微博客户端开发四:微博发送篇(QQ表情,@搜索)
- 调用android手机微博客户端发送微博
- Android 开发调用相机拍照如何获取清晰照片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android应用内使用新浪微博SDK发送微博(不调用微博客户端)
- 调用android手机微博客户端发送微博
- 调用android手机微博客户端发送微博
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- Android开发,调用相册图片或者调用相机进行拍照获取图片
- Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题
- Android开发之调用相机拍照与本地图库选择图片
- 9.腾讯微博Android客户端开发——发送微博
- Android开发基础 调用相机 系统相册(并对图片进行压缩处理)
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android获取gallery中的图片以及调用系统相机
- 9.腾讯微博Android客户端开发——发送微博
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机
- [置顶] Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容