2014-11-20 27 views
0

我正在開發一個應用程序,它會拍攝一張照片,然後將其返回並放入一個imageview中。 我能夠得到它的工作時,它會返回縮略圖大小的圖像。我遵循Android開發者頁面上的指示創建一個圖像文件,但由於某種原因,它不適合我。我有一個按鈕,在啓動intent之前啓動一個調用createImageFile()的方法。然而,這種方法失敗,相機甚至從未啓動。創建相機應用程序存儲照片的目錄時出錯

這裏是我的版本:

private File createImageFile() throws IOException 
{ 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss"). 
               format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 

    File storageDir = Environment.getExternalStoragePublicDirectory(
             Environment.DIRECTORY_PICTURES); 

    Log.i("storage: ",storageDir.getAbsolutePath()); 

    File image = File.createTempFile(imageFileName, 
             ".jpg",storageDir); 
    Log.i("filename: ", "afterimage"); 

    FILENAME = "file:" + image.getAbsolutePath(); 

    Log.i("filename: ", FILENAME); 

    return image; 
} 

但是我的代碼永遠不會使得過去的文件圖像= File.createTempFile(映像文件名稱, 「.JPG」,storageDir) 具有因我從來沒有看到以下Log.i ();

我試圖用context.getCacheDir()創建storageDir,並且相機實際上啓動了,但在拍攝照片後,我無法點擊複選標記並返回主活動。

下面是活動的其餘部分如果需要的話:

public class Camera extends Activity 
{ 
private final int PICTURE_ACTIVITY_CODE = 1; 
String FILENAME; 
File f; 
Button b; 
ImageView image; 

@Override 
/** 
* onCreate method. Instantiates buttons and textfield 
* and sets a listener for button that launches launchTakePhoto 
* method. 
*/ 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 
    image = (ImageView) findViewById(R.id.img); 
    b = (Button) findViewById(R.id.b1); 
    b.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      launchTakePhoto(); 
     } 
    }); 
} 

/** 
* method that finds storage location on devices. Then 
* creates a file and launches an actvitiy for result 
* to retrieve the data later and store it in that file, 
* in that storage space. 
*/ 
private void launchTakePhoto() 
{ 

    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if(android.os.Environment.getExternalStorageState().equals(
         android.os.Environment.MEDIA_MOUNTED)) 
    { 
     try 
     { 
      f = createImageFile(); 
     } 
     catch(IOException e) 
     { 
      Log.i("Error","error"); 
     } 
    } 
    else 
    { 
     f = new File(getCacheDir(), FILENAME); 
    } 
    if (f != null) 
    { 
     Uri outputFileUri = Uri.fromFile(f); 
     i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(i, PICTURE_ACTIVITY_CODE); 
    } 
} 

protected void onActivityResult(int req, int res, Intent data) 
{ 

    if ((req == PICTURE_ACTIVITY_CODE)&&(res == RESULT_OK)) 
    { 
      Log.i("res: ", "PASSED"); ImageView iv = (ImageView) findViewById(R.id.img); 
      Uri uriImage = Uri.fromFile(f); 
      iv.setImageURI(uriImage); 
    } 
} 

感謝您的幫助。我見過很多不同的可能答案,但目前爲止我沒有任何工作可以解決。我只是想能夠返回一個全尺寸的圖像,並將其放置在圖像保持器中。

回答

1

我想你可能是最好看的圖像捕獲Android的API,因爲它最近因爲API 21.可能執行此操作的最佳方法是打開攝像頭,允許用戶捕獲圖像和改變使用:http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.CaptureCallback.html中定義的onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)函數來接收圖像捕獲的結果,然後將圖像查看器設置爲結果。如果您還嘗試保存圖像,請嘗試使用Android文檔中定義的文件處理程序進程:http://developer.android.com/training/camera/photobasics.html

String mCurrentPhotoPath; 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
     imageFileName, /* prefix */ 
     ".jpg",   /* suffix */ 
     storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 
相關問題