2012-04-02 11 views
1

在我的應用程序中,我打開相機並想用特定名稱保存該文件。 我用這個代碼:當使用意圖相機時爲圖像命名

public void onLongPress(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("new-photo-name.jpg"))); 
     startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

} 
protected void onActivityResult1(int requestCode, int resultCode, Intent data) { 
        if (requestCode == CAMERA_PIC_REQUEST) { 
          Bitmap image = (Bitmap) data.getExtras().get("data"); 
        } 
} 

它確實打開相機,我可以把和保存照片,但它不會給好名字。 每當我保存圖片時,他都會給圖片另一個名字,例如1名稱爲:「13333675392558.jpg」。我不明白他是如何得到這種數字的。

爲什麼我的代碼不適用名稱:「new-photo-name.jpg」?

和/或我錯了什麼呢?

感謝不已,Bigflow

回答

0

我懂了工作,但不知道確切的同樣的問題還沒有,但是這個代碼爲我工作:

private Uri outputFileUri; 
    public void onLongPress(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File file = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/new-photo-name.jpg"); 

     outputFileUri = Uri.fromFile(file); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, TAKE_PICTURE); 
    } 

onLongPress有事情做與手勢(觸摸)的動作,你也可以使用一個按鈕在這裏。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == TAKE_PICTURE){ 
      System.out.println("string of file name = "+outputFileUri.toString()); 
     } 
} 

真的很小的代碼,但就像一個魅力

1
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

是設置文件名就行了。這是你必須改變的路線。

+0

把它改成:'intent.putExtra(MediaStore.EXTRA_OUTPUT, 「新照片name.jpg」);'但不知何故,這沒」工作。 – Bigflow 2012-04-02 12:22:04

+0

字符串不是Uri。我不知道如果這是最好的,我的應用程序使用Uri.fromFile(新文件(字符串文件名)) – pouzzler 2012-04-02 12:30:54

+0

我改變了代碼,所以它更容易看。但是這也不起作用:( – Bigflow 2012-04-02 12:52:45