android – 用相机拍照并将其保存在特定文件夹中

android – 用相机拍照并将其保存在特定文件夹中,第1张

概述我正在开发一个 Android应用程序,允许用户使用Android摄像头拍摄一些照片.用户拍摄这些照片参加摄影比赛.所以,他拍了几张照片,这些照片应保存到特定的目的地,过了一会儿,他在这些照片之间循环,决定哪一个他将参加摄影比赛. 那么,照片应该保存在不在画廊中的特定文件夹中以及其他不适合比赛的照片中. 目前,我只是将照片保存到SD卡,我不知道如何将它们保存在某个文件夹中. 我必须说我已经建立了自 我正在开发一个 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,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;    }}

如果你能指出我正确的方向,我会赞美它.谢谢

解决方法 只需在sdcard之后添加文件夹名称(即/ sdcard / images / wildlife).要确保指定的文件夹存在,请调用方法 File.mkdirs().请不要使用硬编码的字符串/ SD卡访问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) {        }    }}
总结

以上是内存溢出为你收集整理的android – 用相机拍照并将其保存在特定文件夹中全部内容,希望文章能够帮你解决android – 用相机拍照并将其保存在特定文件夹中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存