2012-04-01 200 views
1

我一直在努力解决这个问题,但没有取得任何成功。保存拍摄图像

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

这里是我的代码:

@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

看看这个环节之一。它可以是有帮助的;) [Android的ACTION_IMAGE_CAPTURE意图] [1] [1]:http://stackoverflow.com/questions/1910608/android-action-image-capture-intent – Imrane 2012-04-01 21:36:52

+0

可能你一定已经解决了你的问题,或者我应该帮忙 – Ishu 2012-04-02 02:27:23

回答

4

尝试下面的代码是解决你的问题::

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(); 
    } 
    } 
}