2012-04-28 36 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) { 
// --- 
} 

希望这个作品......

相关问题