【问题标题】:Android: Renaming images from cameraAndroid:重命名相机中的图像
【发布时间】:2013-09-18 01:57:13
【问题描述】:

目前我在我的应用程序中使用此代码来允许用户拍摄照片。但是我如何更改代码以便在用户拍摄照片后将其重命名为“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);
}
}

【问题讨论】:

    标签: java android camera java-io


    【解决方案1】:

    试试这个。

    //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);
    

    Taken From Here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多