保存捕获的图像

时间:2012-04-01 21:17:22

标签: android

我一直试图解决这个问题,但没有取得任何成功。

我有一个非常简单的应用程序,可以捕获图像并将该图像显示给用户。问题是它会在捕获日期时保存图像,但我想给图像指定一个特定的名称。

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

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

            startActivityForResult(intent, CAMERA_REQUEST); 
        }
    });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    System.gc();
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);

    }  
}

如果有人能告诉我需要添加的内容以及设置图片名称的位置,我将非常感激。

1 个答案:

答案 0 :(得分:5)

尝试以下代码是解决问题的方法之一::

static Uri capturedImageUri=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        Calendar cal = Calendar.getInstance();
File file = new File(Environment.getExternalStorageDirectory(),  (cal.getTimeInMillis()+".jpg"));
    if(!file.exists()){
    try {
        file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }else{
       file.delete();
    try {
       file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    capturedImageUri = Uri.fromFile(file);
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
    startActivityForResult(i, CAMERA_RESULT);
       }
    });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        //Bitmap photo = (Bitmap) data.getExtras().get("data");
        //imageView.setImageBitmap(photo);
        try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(),  capturedImageUri);
    imageView.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }  
}