展会信息港展会大全

android拍照,调用系统相册,相片上传
来源:互联网   发布日期:2015-09-29 10:05:36   浏览:2794次  

导读:测试机为小米,酷派拍照// 启动相机startCamera.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// mCurrentPhotoFile = ......

测试机为小米,酷派

拍照

// 启动相机

startCamera.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

// mCurrentPhotoFile = new File(PHOTO_DIR,getPhotoFileName());

// //用当前时间给取得的图片命名

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

// Intent intent = new

// Intent("android.media.action.IMAGE_CAPTURE");

// Uri fromFile = Uri.fromFile(mCurrentPhotoFile);

// String string = fromFile.toString();

// Log.i("gp", string);

// intent.putExtra(MediaStore.EXTRA_OUTPUT,

// Uri.fromFile(mCurrentPhotoFile));

startActivityForResult(intent, CAPTURE_CODE);

}

});

调用系统相册

// 查看相片

camera_look_photo.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("image/*");

startActivityForResult(intent, IMAGE_CODE);

}

});

对回调函数进行处理

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

ContentResolver resolver = getContentResolver();

// 拍照

if (resultCode != RESULT_OK)

return;

switch (requestCode) {

case CAPTURE_CODE: {

// Toast.makeText(Camera.this, "--------------", 2).show();

Bitmap set_map = null;

paizhao = true;

xiangce = false;

// doCropPhoto(mCurrentPhotoFile);

String sdStatus = Environment.getExternalStorageState();

Bundle bundle = data.getExtras();

if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {

bitMap = (Bitmap) data.getParcelableExtra("data");

if (bitMap == null) {

Uri originalUri = data.getData();

try {

System.out

.println(Build.MODEL+"----------camera----------------------611-------"

+ originalUri.toString());

byte[] mContent = readStream(resolver

.openInputStream(Uri.parse(originalUri

.toString())));

if(bitMap!=null)bitMap.recycle();

bitMap = getPicFromBytes(mContent, null);

cam_photo.setImageBitmap(bitMap);

camera_paht = "/data/sdcard/DCIM/Camera/"+ getPhotoFileName();

File aFile = new File("/data/sdcard/DCIM/Camera/"); // 图片上传地址

if(!aFile.exists())

{

System.out.println("camera-----------------------630------------创建文件---");

aFile.mkdirs();

}

ByteArrayOutputStream out = new ByteArrayOutputStream();

if(set_map!=null)set_map.recycle();

set_map = scaleImg(bitMap, x[position_s],

y[position_s]);

set_map.compress(Bitmap.CompressFormat.JPEG, 25, out);

photo = out.toByteArray();

out.close();

try {

FileOutputStream fos = new FileOutputStream(camera_paht); // 文件输出流

try {

fos.write(photo);

// bitMap.recycle();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} else {

//bitMap = (Bitmap) bundle.get("data");

Bitmap set_map2 = null;

bitMap = (Bitmap) data.getParcelableExtra("data");

System.out.println(Build.MODEL+"camera----------------660-------"+bitMap);

if (bitMap == null) {

Uri originalUri = data.getData();

System.out.println("camera----------------------687-------"

+ originalUri.toString());

byte[] mContent=null;

try {

mContent = readStream(resolver.openInputStream(Uri

.parse(originalUri.toString())));

if(bitMap!=null)bitMap.recycle();

bitMap = getPicFromBytes(mContent, null);

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

//在这边进行图片的旋转

Bitmap resizedBitmap = null;

if(Build.MODEL.equals("7260")){

Matrix matrix = new Matrix();

matrix.postRotate(270);

resizedBitmap = Bitmap.createBitmap(bitMap, 0, 0,bitMap.getWidth(), bitMap.getHeight(), matrix, true);

bitMap = resizedBitmap;

}

System.out

.println("camera------------------585----------bitmap---"

+ bitMap);

// map = scaleImg(bitMap, 250, 420);

System.out.println("Camera--------------75------");

cam_photo.setImageBitmap(bitMap);

ByteArrayOutputStream out = new ByteArrayOutputStream();

// 对图片上传时候图片大小的选择

if(set_map2!=null)set_map2.recycle();

set_map2 = scaleImg(bitMap, x[position_s],

y[position_s]);

if(set_map2==null)return;

// 将其写入输出流中

set_map2.compress(Bitmap.CompressFormat.JPEG, 25, out);

photo = out.toByteArray();

try {

out.close();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

intTobyte(photo.length); // 图片的长度

System.out.println("camera-----------------------722-------"+photo_len);

camera_paht = PHOTO_DIR + File.separator

+ getPhotoFileName();

System.out

.println("camera ---------照片存储地址----------419-----"

+ camera_paht);

File aFile = new File(camera_paht); // 图片上传地址

try {

FileOutputStream fos = new FileOutputStream(aFile); // 文件输出流

try {

fos.write(photo);

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//System.out.println("-----photo----" + photo.length);

// Toast.makeText(Camera.this, "" + photo.length, 2).show();

}

break;

}

// 相册

case IMAGE_CODE: {

// camera_paht =PHOTO_DIR+File.separator+getPhotoFileName() ;

Bundle bundle = data.getExtras();

System.out.println("Camera--------------75------");

paizhao = false;

xiangce = true;

Uri originalUri = data.getData();

if (originalUri != null) {

Bitmap bitMap = null;

try {

if(bitMap!=null)bitMap.recycle();

bitMap = null;

bitMap = MediaStore.Images.Media.getBitmap(resolver,

originalUri);

String[] proj = { MediaColumns.DATA };

Cursor cursor = managedQuery(originalUri, proj, null, null,

null);

// 按我个人理解 这个是获得用户选择的图片的索引值

int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);

System.out.println("706-------------camera------"

+ column_index);

// 将光标移至开头 ,这个很重要,不小心很容易引起越界

cursor.moveToFirst();

// 最后根据索引值获取图片路径

ByteArrayOutputStream out = new ByteArrayOutputStream();

uploadPhotPath = cursor.getString(column_index);

System.out

.println("camera--------------------561----------- 相册图片的地址--------"

+ uploadPhotPath);

Bitmap map = scaleImg(bitMap, 250, 420);

map.compress(Bitmap.CompressFormat.JPEG, 25, out);

photo = out.toByteArray();

intTobyte(photo.length); // 图片的长度

System.out

.println("camera-----------------565----------相册图片的长度-----"

+ photo.length);

cam_photo.setImageBitmap(map);

} catch (Exception e) {

Toast.makeText(Camera.this, "图片不可用", 3).show();

} // 获取相机返回的数据

}

break;

}

}

}

图片的上传,以前是用特定的协议进行上传,不知道是协议的什么地方出现了,一直上传不成功。在网上找到另一种上传方法,Http有自己封装好的方法,

private void uploadFile2Svr2() {

if (paizhao) {

file = new File(camera_paht);

// paizao图片的地址

}

if (xiangce) {

if(uploadPhotPath==null)

return;

file = new File(uploadPhotPath);

}

System.out.println("camera------------------------807");

// 相册图片的地址

String httpUrl = path;

System.out.println("---------------");

HttpPost request = new HttpPost(httpUrl);

HttpClient httpClient = new DefaultHttpClient();

FileEntity entity = new FileEntity(file, "binary/octet-stream");

HttpResponse response;

request.setEntity(entity);

entity.setContentEncoding("binary/octet-stream");

try {

response = httpClient.execute(request);

System.out.println(response.toString());

} catch (ClientProtocolException e) {

Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();

e.printStackTrace();

}

}

可能有些混乱,可以从当中抽取自己需要的部分,不是所有的都适合自己

赞助本站

人工智能实验室

相关热词: 拍照 调用 上传

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港