andriod调用照相机后不拍照按返回键循环调用相机而无法返回程序页面

邹子尧 发布于 2013/05/10 13:09
阅读 897
收藏 0
package nzy.ssp;


import java.io.File;


import nzy.ssp.net.AuthSina.common.InfoHelper;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


//定义一个类,使其实现拍照并保存功能
public class ActivityTakePic extends Activity {
// 定义类的成员变量


private int TAKE_PICTURE = 1;
private String TAG = "ActivityTakePic";
public static String thisLarge = null;
private String theSmall = null;
public static Uri photoUri;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
   //setContentView(R.layout.main);
Toast.makeText(this, "启动拍照功能……", Toast.LENGTH_SHORT).show();
// 手机拍照
opentakePicDialog();
}


private boolean checkSdCard() {
if (android.os.Environment.getExternalStorageDirectory().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}


// 拍照
private void opentakePicDialog() {
if (!checkSdCard()) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);


} else {
Toast.makeText(ActivityTakePic.this, "你手机上不存在存储卡",
Toast.LENGTH_LONG).show();
}


}


/*
* 相机拍照回调函数

* @see android.app.Activity#onActivityResult(int, int,
* android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
// super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, TAG);
Intent intent = new Intent();


String camerName = InfoHelper.getFileName();
Log.i(TAG, "camerName =       " + camerName);
String fileName = "Share" + camerName + ".jpg";


File camerFile = new File(InfoHelper.getCamerPath(), fileName);


theSmall = InfoHelper.getCamerPath() + fileName;
thisLarge = getLatestImage();
Log.i(TAG, "theSmall =       " + theSmall);
Log.i(TAG, "thisLarge =       " + thisLarge);
photoUri = Uri.fromFile(camerFile);
data.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
photoUri);
intent.putExtras(data);
intent.putExtra("int", 0);
intent.setClass(ActivityTakePic.this, ActivityMain.class);
startActivity(intent);
}
}
}


/**
* 获取SD卡中最新图片路径

* @return
*/
@SuppressWarnings("unused")
protected String getLatestImage() {
String latestImage = null;
String[] items = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, items, null,
null, MediaStore.Images.Media._ID + " desc");


if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
latestImage = cursor.getString(1);
break;
}
}


return latestImage;
}


}
加载中
返回顶部
顶部