当前位置: 代码迷 >> Android >> Android:SNS客户端开发7:发送带图片的微博(一)(调用相机和Gallery获得照片)
  详细解决方案

Android:SNS客户端开发7:发送带图片的微博(一)(调用相机和Gallery获得照片)

热度:95   发布时间:2016-05-01 19:30:25.0
Android:SNS客户端开发七:发送带图片的微博(一)(调用相机和Gallery获得照片)

???? 之前已经介绍了如何发布一条文字微博,接下来的两篇文章会介绍如何发送带图片的微博。今天先看如何调用照相或者Gallery来获取我们想要发送图片文件。

第一步,看需要申明的几个值

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;//拍摄的照片文件

?

第二步,选择获取图片方式的对话框实现

?

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();	}
/*	 * 通过相机回传图片的文件名	 */	private String getPhotoFileName() {		Date date = new Date(System.currentTimeMillis());		SimpleDateFormat dateFormat = new SimpleDateFormat(				"'IMG'_yyyyMMdd_HHmmss");		return dateFormat.format(date) + ".jpg";	}

?

?

?再来看OnActivityResult

?

/*	 * 选择图片的回传处理	 */	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,但是可以通过通用方法,将照相的信息写入到我们制定的文件当中。也就是上面代码中的这两句

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当中,压缩方法如下:

/*	 * 压缩图片,避免内存不足报错	 */	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;	}

?

至此,我们完成了对图片的选择,下一篇文章会通过新浪指定的方法,实现将图片上传到微博,也就是发送一条带图片的微博。

1 楼 codeMoe 2012-03-21  
ntent 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(); 
                                } 

请问这段是在什么时候调用
2 楼 codeMoe 2012-03-21  
codeMoe 写道
ntent 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(); 
                                } 

请问这段是在什么时候调用

额。。不好意思,是我没仔细看,就在那拍照哪里调用
3 楼 codeMoe 2012-03-21  
还有个问题,我显示图片时,为什么都是将本来应该竖着显示的图片,变为横着显示,压缩后照片很多锯齿
  相关解决方案