您的位置:首页 > 移动开发 > Android开发

android 上图片选取器以及图片尺寸太大情况下处理

2012-06-04 20:42 323 查看
代码请见我的资源http://download.csdn.net/detail/samguoyi/4351467

package test.sam.imgpickdemo;

import java.io.File;

import javax.security.auth.PrivateCredentialPermission;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class PickPictureDemoActivity extends Activity implements View.OnClickListener
{
private static final int ACTIVITY_PICKLOCAL = 0;
private static final int ACTIVITY_PICKCAMERA = 1;

private ImageView showImageView = null;
private File imgFile = null;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.main_pickcamera).setOnClickListener(this);
findViewById(R.id.main_picklocal).setOnClickListener(this);
showImageView = (ImageView)findViewById(R.id.main_showimg);
}

public void onClick(View v)
{
switch (v.getId()) {
case R.id.main_picklocal:
/*
* 使用这种intent则调用任何注册过的图片浏览器,例如es文件浏览器,来选取图片
*/
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
/*
* 使用这种方式只调用系统的图库程序来选取图片
*/
//Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_PICKLOCAL);
break;
case R.id.main_pickcamera:
imgFile = new File(Environment.getExternalStorageDirectory(), "testpick"+System.currentTimeMillis()+".jpg");
try {
imgFile.createNewFile();
} catch (Exception e) {
}
if(imgFile.exists())
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(intent, ACTIVITY_PICKCAMERA);
}

break;
default:
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("resultCode = "+resultCode);
String photopathString = null;

if(resultCode == RESULT_OK)
{
if(requestCode == ACTIVITY_PICKLOCAL)
{
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
photopathString = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
Bitmap bitmap = BitmapFactory.decodeFile(photopathString);
showImageView.setImageBitmap(bitmap);
cursor.close();

/*
* 这段代码可以获取图片的方向,以决定图片是横向放置还是竖向放置
*/
try {
ExifInterface exifInterface = new ExifInterface(photopathString);
String tag =  exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println("tag = "+tag);
} catch (Exception e) {
e.printStackTrace();
}

/*
* 这段代码可以将图片的长宽等信息decode出来而不需要分配内存
*/
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photopathString, options);
System.out.println("options.outWidth = "+options.outWidth);
System.out.println("options.outHeight = "+options.outHeight);
} catch (Exception e) {
}

/*
* 使用这段代码可以缩小取得图片尺寸options.inSampleSize为几,则取得的图片大小为原来的几分之一
* 注意inSampleSize需要为2的幂次方
*/
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap2 =  BitmapFactory.decodeFile(photopathString, options);
System.out.println("bitmap2.getWidth() = "+bitmap2.getWidth());
System.out.println("bitmap2.getHeight() = "+bitmap2.getHeight());
} catch (Exception e) {
}

}

if(requestCode == ACTIVITY_PICKCAMERA)
{
/*
* 从摄像头返回的图片无法从data.getData()中取得?不知道为啥
*/
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
System.out.println(bitmap.getWidth());
System.out.println(bitmap.getHeight());
showImageView.setImageBitmap(bitmap);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐