0

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

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();
        }};
4

1 回答 1

0

现在将此代码添加到您的 onPictureTaken Date = new Date(); 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";
}
于 2013-06-28T11:57:23.890 回答