0

目前我在我的应用程序中使用此代码来允许用户拍摄照片。但是我如何更改代码以便在用户拍摄照片后将其重命名为“xxxx01.jpg”等名称?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.photo);

    _image = (ImageView) findViewById(R.id.image);
    _field = (TextView) findViewById(R.id.field);
    _button = (Button) findViewById(R.id.button);
    _button.setOnClickListener(new ButtonClickHandler());

    _path = Environment.getExternalStorageDirectory()
            + "/images/make_machine_example.jpg";
}

public class ButtonClickHandler implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        Log.i("MakeMachine", "ButtonClickHandler.onClick()");
        startCameraActivity();
    }
}

protected void startCameraActivity() {
    Log.i("MakeMachine", "startCameraActivity()");
    File file = new File(_path);
    Uri outputFileUri = Uri.fromFile(file);

    Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("MakeMachine", "resultCode: " + resultCode);
    switch (resultCode) {
    case 0:
        Log.i("MakeMachine", "User cancelled");
        break;

    case -1:
        onPhotoTaken();
        break;
    }
}

protected void onPhotoTaken() {
    Log.i("MakeMachine", "onPhotoTaken");

    _taken = true;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

    _image.setImageBitmap(bitmap);

    _field.setVisibility(View.GONE);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i("MakeMachine", "onRestoreInstanceState()");
    if (savedInstanceState.getBoolean(PhotoCaptureExample.PHOTO_TAKEN)) {
        onPhotoTaken();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(PhotoCaptureExample.PHOTO_TAKEN, _taken);
}
}
4

1 回答 1

0

尝试这个。

//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

filePath = "/MyImages/QR_" + timeStamp + ".png" ;
File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

取自这里。

于 2013-09-18T03:14:28.347 回答