开发者

doing photos with the camera and save them in a specific folder

I'm working on an android application which allows the user to take some photos using the android camera.The user takes this pictures for competing to a photo contest.So, he takes a few photos, which should be saved into a specific destination and after a while he loops between those photos and decide with which one he will compete to the photo contest. Well, for that the photos should be saved on a specific folder not in the gallery among other photos which are not for the contest. Currently, I'm just saving the photos to SDcard and I don't know how should I do to save them in a certain folder.

I must say that I have already built my own camera but still don't know how to act when comes to saving the images.

And here is how it looks like:

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 开发者_Go百科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;
    }



}

If you could point me in the right direction I would apppreciate it.Thanks


Just add the folder name after sdcard (i.e. /sdcard/images/wildlife). To ensure that the specified folder exist, call the method File.mkdirs(). And please don't use the hard coded string /sdcard to access SDCard, use Environment.getExternalStorageDirectory method. Revert back for any query.

Edit:

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) {
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜