用相机拍照并将它们保存在特定的文件夹中

时间:2011-08-01 07:06:40

标签: android camera photo

我正在开发一个Android应用程序,允许用户使用Android相机拍摄一些照片。用户拍摄这些照片参加照片竞赛。所以,他拍了几张照片,应保存到具体的目的地,过了一会儿,他在这些照片之间循环,并决定他将参加哪一场比赛。 那么,照片应该保存在不在画廊中的特定文件夹中以及其他不适合比赛的照片中。 目前,我只是将照片保存到SD卡,我不知道如何将它们保存在某个文件夹中。

我必须说我已经制作了自己的相机,但在保存图像时仍然不知道如何采取行动。

以下是它的样子:

public class EditPhoto extends Activity implements SurfaceHolder.Callback, OnClickListener {
    static final int FOTO_MODE = 0;
    private static final String TAG = "CameraTest";
    Camera mCamera;
    boolean mPreviewRunning = false;
    private Context mContext = this;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //doing things
   }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){
        public void onPictureTaken(byte[] imageData, Camera c) {

            if (imageData != null) {

                Intent mIntent = new Intent();

                StoreByteImage(mContext, imageData, 50, "ImageName");
                mCamera.startPreview();

                Bundle b = new Bundle();
                b.putByteArray("imageData", imageData);
                Intent i = new Intent(mContext, ImageDisplayActivity.class);
                i.putExtras(b);
                startActivity(i);

                setResult(FOTO_MODE, mIntent);
                finish();

            }
        }
    };



    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    protected void onStop() {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.e(TAG, "surfaceChanged");
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        List<Size> sizes = p.getSupportedPictureSizes();

        p.setPreviewSize(640, 480);
        p.setPictureSize(213,350);

       mCamera.setParameters(p);
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

     public void onClick(View arg0) {

        mCamera.takePicture(null, mPictureCallback, mPictureCallback);

    }

    public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

        File sdImageMainDirectory = new File("/sdcard");
        FileOutputStream fileOutputStream = null;
        String nameFile;
        try {

            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 5;
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
            fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/image.jpg");
            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

            myImage.compress(CompressFormat.JPEG, quality, bos);

            bos.flush();
            bos.close();

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

        return true;
    }



}

如果你能指出我正确的方向,我会批评它。谢谢

1 个答案:

答案 0 :(得分:7)

只需在sdcard之后添加文件夹名称(即/ sdcard / images / wildlife)。要确保指定的文件夹存在,请调用方法File.mkdirs()。请不要使用硬编码字符串/sdcard来访问SDCard,请使用Environment.getExternalStorageDirectory方法。恢复任何查询。

修改

public class ImageViewActivity extends Activity {

    private String[] imageDirs;
    private Spinner dirSpinner;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getDirs();

        populateSpinner();

    }

    private void populateSpinner() {
        dirSpinner = (Spinner) findViewById(R.id.dirSpinner);
        ArrayAdapter<String> dirAdapter = new ArrayAdapter<String>(
                getApplicationContext(),
                android.R.layout.simple_spinner_dropdown_item, imageDirs);

        dirAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dirSpinner.setAdapter(dirAdapter);
        dirSpinner.setOnItemSelectedListener(new OnImageItemSelectedListener());
    }

    /**
     * Retrieves all the image files in the given directory.
     */
    public File[] retrieveContents(String dirPath) {

        File parentDir = new File(dirPath);

        if (!parentDir.exists()) {
            return null;
        }

        if (!parentDir.isDirectory()) {
            return null;
        }

        File[] fileContents = null;

        FilenameFilter filter = new FilenameFilter();
        fileContents = parentDir.listFiles(filter);

        filter = null;

        parentDir = null;

        return fileContents;
    }

    /**
     * Inner class to get images only.
     */
    private class FilenameFilter implements FileFilter {
        @Override
        public boolean accept(File dir) {
            return dir.getName().toLowerCase()
                    .endsWith(".jpg;*.bmp;*.png;*.gif");
        }
    }

    /**
     * Returns the names of sub-dirs in Images dir.
     */
    private void getDirs() {
        String parentDir = Environment.getExternalStorageDirectory()
                + "/Images";

        File[] imageFolders = new File(parentDir).listFiles();
        ArrayList<String> dirList = new ArrayList<String>();

        for (int i = 0; i < imageFolders.length; i++) {
            if (imageFolders[i].isDirectory()) {
                dirList.add(imageFolders[i].getName());
            }
        }

        imageDirs = (String[]) dirList.toArray(new String[0]);
    }

    public class OnImageItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {

            Toast.makeText(
                    parent.getContext(),
                    "The dir is " + parent.getItemAtPosition(position).toString(),
                    Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}