2012-04-28 60 views
2

嗨,大家好我正在使用相機應用程序窗體我的活動。使用下面的這一行,我試圖將圖像輸出到我在SD卡上的指定位置。它不會將圖像保存到它從getImageUri()獲取的位置,而是將文件保存到相機gallery.any想法出了什麼問題。相機不保存到指定的文件位置android

intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri()); 

//my getImageUri 

private Uri getImageUri() { 

    // Store image on sdcard 

    String dir= Environment.getExternalStorageDirectory() +"/my_app/Datapics"; 

    File dirs = new File(dir); 
    if (!dirs.exists()) { 
     dirs.mkdirs(); 
    } 
    Bundle extras = getIntent().getExtras(); 
    CAPTURE_TITLE = extras.getString(some.NAME); 
    //EDIT if i add this line here 
    CAPTURE_TITLE= "whatever.png"; 
    //it will save my picture to the folder i want it to save to but with name 
    //whatever.png i'm getting my name from my previous activity and want 
    //to assign it to capture title 

    File file = new File(dir , CAPTURE_TITLE+".jpg"); 

    Uri imgUri = Uri.fromFile(file); 

    Log.e("get imageuri called: ",imgUri.toString()); 
//this is what I get from here 
//04-28 19:42:33.835: E/get imageuri called:(2049): 
//file:///mnt/sdcard/my_app/Datapics/BlackbirdSat%20Apr%2028%2019%3A42.jpg 


    Log.e("get imageuri called2: ",file.toString()); 
    //this is what i get here 
//04-28 19:42:33.835: E/get imageuri called2:(2049) 
//:/mnt/sdcard/my_app/Datapics/BlackbirdSat Apr 28 19:42.jpg 
    return imgUri; 

} 
+0

以及我條紋本次活動並創建了單獨的應用程序,我將CAPTURE_TITLE指定爲私有靜態最終的應用程序,並將具有一個活動的應用程序將該文件保存到我想要的sdcard中。任何解決方法。 – 2012-04-28 23:10:55

+0

哦,只是我的想象力,它沒有最終 – 2012-04-28 23:17:54

+0

所有的人我不知道這一點。名稱不能包含:我正在使用包含的日期和時間來構建名稱,所以我使用了新的Date()。toString()。replace(「:」,「」);並將我的照片保存到我的位置。 – 2012-04-29 00:59:38

回答

0

我已經看到了Android和/或硬件的某些版本不尊重MediaStore.EXTRA_OUTPUT標誌。我有這樣的評論+解決方案在我的代碼:

/* 
* HTC Eris and possibly other phones DONT respect the MediaStore.EXTRA_OUTPUT flag and they only 
* return the image URI in the Data. In Android instances where the MediaStore.EXTRA_OUTPUT *IS* respected 
* then Data is null. So use this fact retrieve the correct bitmap reference. When we have to return it from 
* Data then just copy it to the fileUri that we tried to store it in the first place via 
* MediaStore.EXTRA_OUTPUT. 
*/ 


    private void gotoCamera() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    String mBoothFileName = "snap_" + (new Date()).getTime() + ".jpg"; 
    mStateHolder.mPictureFile = new File(((BatchApp) getApplication()).getStorageDirectory(), mBoothFileName); 
    mStateHolder.mPictureUri = Uri.fromFile(mStateHolder.mPictureFile); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mStateHolder.mPictureUri); 
    startActivityForResult(intent, TAKE_PICTURE); 
    } 


/* 
* When the post Camera activity returns 
*/ 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == TAKE_PICTURE) { 
    if (resultCode == RESULT_OK) { 
     /* 
      * HTC Eris and possibly other phones DONT respect the MediaStore.EXTRA_OUTPUT flag and they only 
      * return the image URI in the Data. In Android instances where the MediaStore.EXTRA_OUTPUT *IS* respected 
      * then Data is null. So use this fact retrieve the correct bitmap reference. When we have to return it from 
      * Data then just copy it to the fileUri that we tried to store it in the first place via 
      * MediaStore.EXTRA_OUTPUT. See how sneaky we are.... 
      */ 
     if (data != null && data.getData() != null) { 
     Uri imageUri = data.getData(); 
     try { 
      InputStream input = getContentResolver().openInputStream(imageUri); 
      FileOutputStream output = new FileOutputStream(mStateHolder.mPictureFile); 
      /* 
      From Apache Commons IO: copy one stream to another: 
      http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html 
      */ 

      IOUtils.copy(input, output); 
      mStateHolder.mPictureUri = Uri.fromFile(mStateHolder.mPictureFile); 
     } catch (Exception e) { 
      Toast.makeText(this, "Oops - couldn't capture your picture.", Toast.LENGTH_LONG).show(); 
     } 
     } 
    } 

    // you now have your image at "mStateHolder.mPictureUri" 
    // do whatever you need to do ... 

    } 
} 
+0

我試了一下,如果沒有得到triggred的聲明,所以看着data.tostring()和data.getData()。tostring()在try catch中的數據是什麼,並且我得到了nullpointerexception。 – 2012-04-28 19:44:31

+0

IOUtils.copy(輸入,輸出)。如果您評論此行以解釋此行解決了什麼問題,那就太好了。 – 2013-05-26 22:57:15

+0

@AndrewS好點 - 我用代碼更新了代碼 – 2013-05-27 16:05:13

0

你需要明確提到,你想要把你的相機圖像獲取SD卡的地址後,文件夾名。在這裏,我把「我的文件夾」裏的圖像將被保存...

試試這個...

File defaultDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
// now specifing custom folder name in defaultDir 
File myDir = new File(defaultDir, "My Folder"); 
// specify your picture name 
String myPictureName = "Picture" + ".jpg"; 
// Making file name 
String filename = myDir.getPath() + File.separator + myPictureName; 
File pictureFile = new File(filename); 
try { 
    //Writing file to SD-Card 
    FileOutputStream fos = new FileOutputStream(pictureFile); 
    fos.write(data); 
    fos.close(); 
} catch (Exception e) { 
// --- 
} 

希望這個作品......

相關問題