2013-01-12 102 views
0

我的程序捕獲圖像(位圖),但我不知道(也找不到)如何使用自定義名稱將其保存在自定義路徑中...這裏是我的圖像捕捉代碼。我可以做嗎?或者有什麼辦法將它另存爲另一種數據類型,而不是位圖?一些幫助將會很大。謝謝。保存位圖在自定義路徑和自定義名稱

public class ShowMessagesPage extends Activity { 
final static int CAMERA_RESULT = 0; 
DBAdapter db = new DBAdapter(this); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_messages_page); 

    Button userButton = (Button)findViewById(R.id.button1); 
    userButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     error(); 
     }}); 

    Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto); 
    buttonPhoto.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(i, CAMERA_RESULT); 
     }});} 

protected void onActivityResult(int requestCode, int resultCode, 
      Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     if (resultCode == RESULT_OK) { 
      Bundle extras = intent.getExtras(); 
      Bitmap bmp = (Bitmap) extras.get("data"); 



     } 
     } 

and the other codes.......... 
+0

[http://marakana.com/forums/android/examples/39.html](go) –

回答

1

試試這個:

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE"); 

File cameraFolder; 

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice"); 
else 
    cameraFolder= ShowMessagesPage.this.getCacheDir(); 
if(!cameraFolder.exists()) 
    cameraFolder.mkdirs(); 

File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg"); 
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
initialURI = Uri.fromFile(photo); 

startActivityForResult(getCameraImage, CAMERA_RESULT); 

而在onActivityResult(),處理Uri initialURI,讓您的圖片這是已經保存到您在上面貼的代碼選擇的道路。圖像文件的名稱將爲:camera_snap.jpg

注意:Uri initialURI是全局聲明的。

+0

很抱歉,Eclipse表示沒有像「StatusUpdate」和「reqcdCameraImage」這樣的方法或變量......還有對他們來說是沒有進口的:/ –

+0

@ArdaOğulÜçpınar:我的不好,我粘貼了我的代碼,並忘記了這些修改。檢查編輯。 –

+0

OMG:D我應該看到它:D thanx:D –