2012-07-21 42 views
2
訪問相機

什麼,我試圖做的是創建一個新的文件夾,並節省相機在folder.I拍攝的圖像正在嘗試使用以下snipet實現它:使用ACTION_IMAGE_CAPTURE

public class DocsAdd extends Activity 
{ 
    private static int TAKE_PICTURE = 1; 
    private Uri outputFileUri; 
    Bitmap thumbnail; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     File filename; 
     try 
     { 
      long currentTime = System.currentTimeMillis(); 
      String fileName = "img" + currentTime+".jpg"; 
      String path = Environment.getExternalStorageDirectory().toString();    
      filename = new File(path + "/AutoistDiary/"+ fileName); 
      FileOutputStream out = new FileOutputStream(filename);    
      out.flush(); 
      out.close();  
      MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(), filename.getName()); 

      outputFileUri = Uri.fromFile(filename); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
     startActivityForResult(intent, TAKE_PICTURE); 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (requestCode == TAKE_PICTURE) 
     { 
      Uri imageUri = null;    
      if (data!=null) 
      { 
       if (data.hasExtra("data")) 
       { 
        thumbnail = data.getParcelableExtra("data"); 
        Intent docsShow_intent=new Intent(this,DocsShow.class); 
        startActivity(docsShow_intent); 
       } 
      } 
      else 
      { 
      } 

     } 
    } 
} 

但是當我運行它在Optimus1上沒有返回任何結果給我的活動,遇到了一個鏈接Camera on Android Example,其中指出我目前面臨的同樣的問題,所以可以幫助我。 ?

+0

下面的答案是否適合您? – 2013-07-21 02:07:28

+0

爲什麼不試一試就試試@JeremyWest大多是的,它正在工作......! – sankettt 2013-07-22 04:35:59

+1

僅供參考,答案旁邊有一個複選標記,如果有人對你有用,你可以點擊它並接受答案。在StoreImage(this,Uri.parse(outputFileUri),puc_img)給出錯誤 – 2013-07-29 17:40:00

回答

4

不幸的是,某些設備上存在一個錯誤,導致onActivityResult中的參數Intend data在您爲攝像機使用MediaStore.EXTRA_OUTPUT標誌時爲空。解決方法是將outputFileUri保持爲全局變量,以便您可以在onActivityResult方法中再次訪問它。例如:

Uri outputFileUri; //global variable 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File photo= new File(Environment.getExternalStorageDirectory(), "Autoist Diary"); 

if (! photo.exists()) 
{ 
    photo.mkdirs(); 
    if (! photo.mkdirs()) 
    { 
     Log.i("MyCameraApp", "failed to create directory"); 
    } 
} 
puc_img=new File(photo,"Puc_Img.jpg"); 
outputFileUri = Uri.fromFile(photo); 
intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri); 
StartActivityForResult(intent, TAKE_PICTURE); 

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) 
    { 
     case TAKE_PICTURE: 
      if (resultCode == Activity.RESULT_OK) 
      {     
       if(data != null) { //if the intent data is not null, use it 
        puc_image = getImagePath(Uri.parse(data.toURI())); //update the image path accordingly 
        StoreImage(this,Uri.parse(data.toURI()),puc_img); 
       } 
       else { //Use the outputFileUri global variable 
        StoreImage(this,outputFileUri,puc_img); 
       } 
      } 
     break; 
    } 
} 
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) 
{ 
    Bitmap bm = null; 
    try 
    { 
     bm = Media.getBitmap(mContext.getContentResolver(), imageLoc); 
     FileOutputStream out = new FileOutputStream(imageDir); 
     bm.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     bm.recycle(); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
public String getImagePath(Uri uri) { 
    String selectedImagePath; 
    // 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if (cursor != null) { 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     selectedImagePath = cursor.getString(column_index); 
    } else { 
     selectedImagePath = null; 
    } 

    if (selectedImagePath == null) { 
     // 2:OI FILE Manager --- call method: uri.getPath() 
     selectedImagePath = uri.getPath(); 
    } 
    return selectedImagePath; 
} 

此變通辦法適用於我。希望有所幫助。

+0

; Uri.parse()解析字符串而不是Uri.so我應該傳遞給這個方法 – sankettt 2012-07-23 09:11:42

+0

@sankettt對不起我的錯。我已經更新了我的答案。你已經是'StoreImage'方法的正確'Uri'。 – Angelo 2012-07-23 09:46:07

+0

該代碼工作正常,但我面臨一個奇怪的問題..保存在我提到的設備上的文件是0kb ..這可能是什麼問題。但與此同時,當我嘗試在其他設備上正常工作。可能是這是2.2的錯誤..任何關於這個 – sankettt 2012-10-22 07:23:52

相關問題