嗨,伙计们,我正在使用相机应用程序形成我的活动。我正在尝试将图像输出到SD卡上的指定位置。它不会将图像保存到从getImageUri()获取的位置,而是将文件保存到相机库。任何想法都会出错。
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 :(得分: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 ...
}
}
答案 1 :(得分: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) {
// ---
}
希望这有效......