将图片从onPictureTaken保存在sd上的我的文件夹中

时间:2013-06-28 11:26:01

标签: android android-layout android-intent

我希望将我的照片保存在我选择的文件夹中;我有以下代码保存在我的dispositive的默认文件夹图像;如何为我的应用修改此代码?

PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        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(FotoActivity.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();
        }};

1 个答案:

答案 0 :(得分:0)

将此代码添加到onPictureTaken中     现在日期=新日期();             long nowLong = now.getTime()/ 9000;             String fname = getPhotoDirectory(MyclassName.this)+“/”+ nowLong +“。jpg”;

        String why = "";
        try {

            File ld = new File(getPhotoDirectory(MyclassName.this));
            if (ld.exists()) {
                if (!ld.isDirectory()){
                    MyclassName.this.finish();
                }
            } else {
                ld.mkdir();
            }

            Log.d(TAG, "open output stream "+fname +" : " +data.length);

            OutputStream os = new FileOutputStream(fname);
            os.write(data,0,data.length);
            os.close();
        } catch (FileNotFoundException ex0) {
            why = ex0.toString();
            ex0.printStackTrace();
        } catch (IOException ex1) {
            why = ex1.toString();
            ex1.printStackTrace();
        }

在您的课程中添加此方法

    public static String getPhotoDirectory(Context context)
{
    //return Environment.getExternalStorageDirectory().getPath() +"/cbo-up";
    return context.getExternalFilesDir(null).getPath() +"/cbo-up";
}