2 u010341948 u010341948 于 2013.05.10 13:00 提问

android这段代码为什么无限启动相机?应该怎么改?
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;
    }

}

1个回答

Baby_Bonnie
Baby_Bonnie   2013.05.10 15:55

在按钮单击事件或其它输入事件使用 opentakePicDialog(); 函数。

u010341948
u010341948 可以详细一点吗?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!