0

我的应用程序是打开内置相机并将其保存到 sd 卡中,然后启动一个新活动并在其上预览捕获的图像。

这是启动新的 AsynTask 以打开相机的 loginActivity :

package com.android.grad;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.Camera.R;

public class LoginActivity extends Activity {

    private final int MEDIA_TYPE_IMAGE = 1;
    private CheckBox rememberMe;
    private EditText userName, passWord;
    private Button loginBtn;
    private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private String path;
    private Uri fileUri;

    private OnClickListener loginOnClick = new OnClickListener() {
        public void onClick(View v) {
            new LoginTask(LoginActivity.this, fileUri).execute();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        path = fileUri.getPath();

        loginBtn = (Button) findViewById(R.id.signInBtn);
        loginBtn.setOnClickListener(loginOnClick);

        rememberMe = (CheckBox) findViewById(R.id.keepMe);

        userName = (EditText) findViewById(R.id.userNameID);
        passWord = (EditText) findViewById(R.id.passwordID);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        try {
            userName.setText(sp.getString("username", ""));
            passWord.setText(sp.getString("password", ""));
            rememberMe.setChecked(sp.getBoolean("rememberMe", false));
        } catch (ClassCastException ex) {
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        Editor editor = sp.edit();
        if (rememberMe.isChecked()) {
            editor.putString("username", userName.getText().toString());
            editor.putString("password", passWord.getText().toString());
        } else {
            editor.putString("username", "");
            editor.putString("password", "");
        }
        editor.putBoolean("rememberMe", rememberMe.isChecked());
        editor.commit();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                Intent intent = new Intent(LoginActivity.this,
                        PreviewActivity.class);
                intent.putExtra("path", path);
                startActivity(intent);

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled the image capture", Toast.LENGTH_LONG)
                        .show();
            } else {
                // Image capture failed, advise user
            }
        }
    }
    /** Create a file Uri for saving an image or video */
    private Uri getOutputMediaFileUri(int type) {
        File f = getOutputMediaFile(type);
        if (f == null) {
            Toast.makeText(LoginActivity.this, "MyCameraApp File not found",
                    Toast.LENGTH_LONG);
            return null;
        }
        return Uri.fromFile(f);
    }

    /** Create a File for saving an image or video */
    private File getOutputMediaFile(int type) {
        // Check if SD card is mounted
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            File externalFilesDir = LoginActivity.this
                    .getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File outFile = new File(externalFilesDir, "IDOCR");

            if (!outFile.exists())
                outFile.mkdirs();

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());

            String path = outFile.getPath() + File.separator + "IMG"
                    + timeStamp + ".jpg";

            File mediaFile = new File(path);
            return mediaFile;
        }
        Toast.makeText(LoginActivity.this, "SD card unmounted",
                Toast.LENGTH_LONG);
        return null;
    }
}

这是 AsynTask :-

package com.android.grad;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;

public class LoginTask extends AsyncTask<Void, Void, Boolean> {

private Activity activity;
private ProgressDialog pd;
private Uri fileUri;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

public LoginTask(Activity activity, Uri fileUri) {
    this.activity = activity;
    this.fileUri = fileUri;
}

@Override
protected void onPreExecute() {
    pd = ProgressDialog.show(activity, "Signing in",
            "Please wait while we are signing you in..");
}

@Override
protected Boolean doInBackground(Void... arg0) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    return true;
}

@Override
protected void onPostExecute(Boolean result) {
    pd.dismiss();
    pd = null;

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    activity.startActivityForResult(intent,
            CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}

这是previewActivity:-

package com.android.grad;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.Camera.R;
import com.OCR.ID.AndroidImage;
import com.OCR.ID.Segement;

public class PreviewActivity extends Activity {

    private ImageView previewIV;
    private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private String path;
    private boolean crop = true;
    private boolean resample = true;
    private ProgressDialog pd;

    OnClickListener processOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                createProgressDialog();
                pd.show();
                new Segement(PreviewActivity.this, pd).execute(path);
            } catch (IOException e) {
            }
        }
    };

    private void createProgressDialog() {
        pd = new ProgressDialog(PreviewActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("Extract ID");
        pd.setMessage("Processing...");
        pd.setIcon(R.drawable.ic_launcher);
        pd.setProgress(0);
        pd.setCancelable(false);
    }

    private OnClickListener backOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            startActivityForResult(
                    new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
                            MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(path))),
                    CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                previewIV.setImageBitmap(BitmapFactory.decodeFile(path));
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview);

        path = getIntent().getExtras().getString("path");

        previewIV = (ImageView) findViewById(R.id.previewPicID);
        previewIV.setImageBitmap(AndroidImage.decodeSampledBitmapFromSDcard(
                path, 150, 150));
        // previewIV.setImageBitmap(AndroidImage.readImage(path));

        Button process = (Button) findViewById(R.id.processID);
        process.setOnClickListener(processOnClickListener);

        Button back = (Button) findViewById(R.id.back);
        back.setOnClickListener(backOnClickListener);
    }
}

有时拍照时出现在previewActivity中有时不出现的错误?

为什么会出现这个错误??

4

0 回答 0