3

可能重复:
如何将相机结果作为数据文件夹中的 uri 获取?

我正在拍摄 Picure,并将其保存在默认文件夹中。

这是我的PictureCallBack()功能:

PictureCallback myPictureCallback_JPG = new PictureCallback(){

        public void onPictureTaken(byte[] arg0, Camera arg1) {       

          Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

          OutputStream imageFileOS;

          try {

           imageFileOS = getContentResolver().openOutputStream(uriTarget);
           imageFileOS.write(arg0);
           imageFileOS.flush();
           imageFileOS.close();

           Toast.makeText(MainActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show();

          } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }

          camera.startPreview();
         }
    };

如何将其保存在特定文件夹中,例如"/sdcard/MyCustonApp/Photos/"

4

4 回答 4

4

添加到您的代码

    String path = Environment.getExternalStorageDirectory().toString();
    File myNewFolder = new File(path + "/your/folder");
    myNewFolder.mkdir();
    File file = new File(path + "/your/folder", name    + ".png");
    OutputStream imageFileOS = new FileOutputStream(file);
于 2012-11-23T13:00:18.083 回答
1

这就是我检查要保存捕获的图像的文件夹是否存在、是否存在或创建文件夹(如果不存在)的方式。

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomApp/Photos");
else
    cameraFolder = StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "MyCustomApp/Photos/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri uri = Uri.fromFile(photo);

startActivityForResult(getCameraImage, 1);

这段代码会将捕获的图像保存到指定的文件夹,名称为camera_snap和扩展名:.jpg

注意:这需要您在Manifest文件中声明以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-11-23T13:01:03.510 回答
1
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); 
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

看看这个链接。如何将Android相机中的图像保存到特定文件夹?.

于 2012-11-23T13:16:09.277 回答
0

试试这个简单的代码并将图像存储在一个文件夹中:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

其中 bm 表示位图图像名称

于 2012-11-23T13:03:20.980 回答